Time Limit: 2 second(s) | Memory Limit: 32 MB |
You are in the world of mathematics to solve the great "MonkeyBanana Problem". It states that, a monkey enters into a diamond shaped twodimensional array and can jump in any of the adjacent cellsdown fromits current position (see figure). While moving from one cell to another, themonkey eats all the bananas kept in that cell. The monkey enters into the arrayfrom the upper part and goes out through the lower part. Find the maximumnumber 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 be2*N - 1 rows. The ith(1 ≤ i ≤ N) line of nextN lines contains exactly inumbers. Then there will beN - 1 lines. The jth (1≤ j < N) line containsN - j integers. Each number isgreater than zero and less than 215.
Output
For each case, print the case number and maximum number ofbananas 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.
题目链接:http://lightoj.com/volume_showproblem.php?problem=1004
题目大意:数字菱形,和数字三角形一样,求路径最大值~
题目分析:上半部分和数字三角形一样,状态由上和上左转移,下半部分状态由上和上右转移
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[205][205];
int a[205][205];
int main()
{
int T;
scanf("%d", &T);
for(int ca = 1; ca <= T; ca++)
{
memset(dp, 0, sizeof(dp));
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= i; j++)
scanf("%d", &a[i][j]);
for(int i = n + 1; i <= 2 * n - 1; i++)
for(int j = 1; j <= 2 * n - i; j++)
scanf("%d", &a[i][j]);
dp[1][1] = a[1][1];
for(int i = 2; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
dp[i][j] = max(dp[i][j], dp[i - 1][j] + a[i][j]);
dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + a[i][j]);
}
}
for(int i = n + 1; i <= 2 * n - 1; i++)
{
for(int j = 1; j <= n; j++)
{
dp[i][j] = max(dp[i][j], dp[i - 1][j] + a[i][j]);
dp[i][j] = max(dp[i][j], dp[i - 1][j + 1] + a[i][j]);
}
}
printf("Case %d: %d\n", ca, dp[2 * n - 1][1]);
}
}