http://www.lightoj.com/volume_showproblem.php?problem=1004
| Time Limit: 2 second(s) | Memory Limit: 32 MB |
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.

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 |
代码是世界上最优美的语言
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <string>
#define SI(T)int T;scanf("%d",&T)
using namespace std;
const int SIZE=1e4+10;
const int maxn=1<<30;
int dp[101];
int a[101];
int main()
{
SI(T);
for(int cas=1;cas<=T;cas++){
int n;
scanf("%d",&n);
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++){
for(int k=1;k<=i;k++)
scanf("%d",&a[k]);
for(int k=i;k>=1;k--)
dp[k]=max(dp[k],dp[k-1])+a[k];
}
for(int i=n-1;i>0;i--){
for(int k=1;k<=i;k++){
scanf("%d",&a[k]);
dp[k]=max(dp[k],dp[k+1])+a[k];
}
}
printf("Case %d: %d\n",cas,dp[1]);
}
return 0;
}
本文介绍了解决猴子吃香蕉问题的最优算法。通过输入描述构建的二维钻石形矩阵,猴子从顶部进入并从底部离开,算法计算并输出猴子能够吃到的最大香蕉数量。详细分析了输入格式、输出要求及算法实现过程。


3023

被折叠的 条评论
为什么被折叠?



