1004-monkey Banana Problem
|
PDF (中文版) |
Statistics |
Forum |
Time Limit: 2 second (s) |
Memory Limit: MB |
Mathematics to solve the great "monkeybanana problem". It states that, a monkey enters to a diamond shaped twodimensional array and can jump in any of the adjacent cells do WN fromits Current position (see figure). While moving from one cell to another, Themonkey eats all of 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'll be2*n-1 rows. The ith (1≤i≤n) line of nextN lines contains exactlyinumbers. Then there'll 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
The Dataset is huge, and the use faster I/O methods.
Title Link: http://lightoj.com/volume_showproblem.php?problem=1004
The main problem: to find the maximum weight from the top down path.
problem-solving ideas: The deformation of the digital triangle, the diamond is divided into two parts to find. DP[I][J] represents the maximum weight and of the path to the J point of Line I. first enter the positive triangle, j=1: dp[i][j] = dp[i-1][j];j=n: dp[i][j] = dp[i-1][j-1]; j=2~n-1: Dp[i][j]=max (dp[i-1][j],dp[i-1][j-1]); then input inverted triangle, Dp[i][j]=max (dp[i-1][j],dp[i-1][j+1]);
The code is as follows:
#include <cstdio> #include <algorithm>using namespace Std;int a[102][102],dp[102][102];int main () { int i,j,t,n,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]); DP[1][1]=A[1][1]; for (i=2;i<=n;i++) for (j=1;j<=i;j++) { if (j==1) dp[i][j]=a[i][j]+dp[i-1][j]; else if (j==n) dp[i][j]=a[i][j]+dp[i-1][j-1]; else Dp[i][j]=a[i][j]+max (dp[i-1][j-1],dp[i-1][j]); } for (i=n-1;i>=1;i--) for (j=1;j<=i;j++) scanf ("%d", &a[i][j]); for (i=n-1;i>=1;i--) for (j=1;j<=i;j++) Dp[i][j]=a[i][j]+max (dp[i+1][j],dp[i+1][j+1]); printf ("Case%d:%d\n", ++cnt,dp[1][1]); } return 0;}
Lightoj 1004 Monkey Banana problem (DP digital triangle)