lightoj 1004 - Monkey Banana Problem
2016-06-06 00:28
453 查看
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.
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<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
int num[201][201];
int main()
{
int T;
scanf("%d",&T);
int icase=0;
while(T--)
{
int n;
scanf("%d",&n);
memset(num,0,sizeof(num));
for(int i=1;i<=n;++i)
{
for(int j=1;j<=i;++j)
{
scanf("%d",&num[i][j]);
}
}
for(int i=n+1;i<=2*n-1;++i)
{
for(int j=1;j<=2*n-i;++j)
{
scanf("%d",&num[i][j]);
}
}
for(int i=2;i<=n;++i)
{
for(int j=1;j<=i;++j)
{
num[i][j]+=max(num[i-1][j-1],num[i-1][j]);
}
}
for(int i=n+1;i<=2*n-1;++i)
{
for(int j=1;j<=2*n-1;++j)
{
num[i][j]+=max(num[i-1][j],num[i-1][j+1]);
}
}
printf("Case %d: %d\n",++icase,num[2*n-1][1]);
}
return 0;
}
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 integer 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<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
int num[201][201];
int main()
{
int T;
scanf("%d",&T);
int icase=0;
while(T--)
{
int n;
scanf("%d",&n);
memset(num,0,sizeof(num));
for(int i=1;i<=n;++i)
{
for(int j=1;j<=i;++j)
{
scanf("%d",&num[i][j]);
}
}
for(int i=n+1;i<=2*n-1;++i)
{
for(int j=1;j<=2*n-i;++j)
{
scanf("%d",&num[i][j]);
}
}
for(int i=2;i<=n;++i)
{
for(int j=1;j<=i;++j)
{
num[i][j]+=max(num[i-1][j-1],num[i-1][j]);
}
}
for(int i=n+1;i<=2*n-1;++i)
{
for(int j=1;j<=2*n-1;++j)
{
num[i][j]+=max(num[i-1][j],num[i-1][j+1]);
}
}
printf("Case %d: %d\n",++icase,num[2*n-1][1]);
}
return 0;
}
相关文章推荐
- 详解Android应用中屏幕尺寸的获取及dp和px值的转换
- 基于Android中dp和px之间进行转换的实现代码
- Android中dip、dp、sp、pt和px的区别详解
- LFC1.0.0 版本发布
- Android dpi,dip,dp的概念以及屏幕适配
- Android px、dp、sp之间相互转换
- HP data protector软件学习1--基本角色与基本工作流程
- HP data protector软件学习2--软件组成与界面介绍
- android中像素单位dp、px、pt、sp的比较
- Android对px和dip进行尺寸转换的方法
- Android根据分辨率进行单位转换-(dp,sp转像素px)
- android 尺寸 dp,sp,px,dip,pt详解
- DP问题各种模型的状态转移方程
- POJ-1695-Magazine Delivery-dp
- nyoj-1216-整理图书-dp
- TYVJ1193 括号序列解题报告
- 对DP的一点感想
- TYVJ上一些DP的解题报告
- soj1005. Roll Playing Games
- 01背包问题