Light oj 1004 - Monkey Banana Problem(DP)
2016-09-04 15:52
405 查看
题目链接:http://lightoj.com/volume_showproblem.php?problem=1004
1004 - Monkey Banana Problem
You are in the world of mathematics to solve the great "Monkey Banana Problem". It states that, a monkey enters into a diamond shaped two dimensional array and can jump in any of the adjacent cells down from its current position (see figure).
While moving from one cell to another, the monkey eats all the bananas kept in that cell. The monkey enters into the array from the upper part and goes out through the lower part. Find the maximum number of bananas the monkey can eat.
4000
teger T (≤ 50), denoting the number of test cases.
Every case starts with an integer N (1 ≤ N ≤ 100). It denotes that, there will be 2*N - 1 rows. The ith (1 ≤ i ≤ N) line of next N lines contains exactly i numbers.
Then there will be N - 1 lines. The jth (1 ≤ j < N) line contains N - j integers. Each number is greater than zero and less than 215.
题目大意:求从上到下路径上的最大值
解析:简单的DP
代码如下:
#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<set>
#include<string>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#define N 1009
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef long long LL;
int dp
, a
;
int main()
{
int t, n, i, j, cnt = 0;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
for(j = 1; j <= i; j++)
scanf("%d", &a[i][j]);
}
int k = 2 * n - 1, nn = n;
for(i = n + 1; i <= k; i++)
{
nn--;
for(j = 1; j <= nn; j++)
scanf("%d", &a[i][j]);
}
memset(dp, 0, sizeof(dp));
for(i = 1; i <= n; i++)
{
for(j = 1; j <= i; j++)
{
dp[i][j] = max(dp[i - 1][j - 1], dp[i - 1][j]) + a[i][j];
}
}
for(i = n + 1; i <= k; i++)
{
for(j = 1; j <= n; j++)
dp[i][j] = max(dp[i - 1][j + 1], dp[i - 1][j]) + a[i][j];
}
printf("Case %d: %d\n", ++cnt, dp[k][1]);
}
return 0;
}
1004 - Monkey Banana Problem
PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
While moving from one cell to another, the monkey eats all the bananas kept in that cell. The monkey enters into the array from the upper part and goes out through the lower part. Find the maximum number of bananas the monkey can eat.
Input
Input starts with an in4000
teger T (≤ 50), denoting the number of test cases.
Every case starts with an integer N (1 ≤ N ≤ 100). It denotes that, there will be 2*N - 1 rows. The ith (1 ≤ i ≤ N) line of next N lines contains exactly i numbers.
Then there will be N - 1 lines. The jth (1 ≤ j < N) line contains N - j integers. Each number is greater than zero and less than 215.
Output
For each case, print the case number and maximum number of bananas eaten by the monkey.Sample Input | Output for Sample Input |
2 4 7 6 4 2 5 10 9 8 12 2 2 12 7 8 2 10 2 1 2 3 1 | Case 1: 63 Case 2: 5 |
Note
Dataset is huge, use faster I/O methods.题目大意:求从上到下路径上的最大值
解析:简单的DP
代码如下:
#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<set>
#include<string>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#define N 1009
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef long long LL;
int dp
, a
;
int main()
{
int t, n, i, j, cnt = 0;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
for(j = 1; j <= i; j++)
scanf("%d", &a[i][j]);
}
int k = 2 * n - 1, nn = n;
for(i = n + 1; i <= k; i++)
{
nn--;
for(j = 1; j <= nn; j++)
scanf("%d", &a[i][j]);
}
memset(dp, 0, sizeof(dp));
for(i = 1; i <= n; i++)
{
for(j = 1; j <= i; j++)
{
dp[i][j] = max(dp[i - 1][j - 1], dp[i - 1][j]) + a[i][j];
}
}
for(i = n + 1; i <= k; i++)
{
for(j = 1; j <= n; j++)
dp[i][j] = max(dp[i - 1][j + 1], dp[i - 1][j]) + a[i][j];
}
printf("Case %d: %d\n", ++cnt, dp[k][1]);
}
return 0;
}
相关文章推荐
- [bat]批量替换文件头部和尾部
- 关于处理Android 异常NetworkOnMainThreadException解决方案
- jQuery实现智能判断固定导航条或侧边栏的方法
- 设计模式实现——观察者模式
- spring添加事务管理
- 题目:旋转数组的最小数字
- MESPRO
- Android开发 Fiddler抓包
- 插入排序算法
- poj 3233Matrix Power Series(矩阵快速幂 二分求和 求累乘的和)
- ios开发之核心动画四:核心动画-Core Animation--CABasicAnimation基础核心动画
- 数学 求平行四边形的面积和坐标
- MyBatis学习之二:SQL语句映射文件(1)resultMap
- hadoop HA原理
- 浅析智能指针一
- xpath语法
- Python迭代器笔记
- 练习:WinForm 进程(创建、注销)
- gocode+auto-complete搭建emacs的go语言自动补全功能
- [bat] 批量删除文本文件的空行