You areinchThe world's mathematics to solve the great"Monkey Banana Problem". It states that, a monkey enters into a diamond shaped the dimensional array and can jumpinchAny of the adjacent cells down fromIts current position (see figure). While moving fromOne cell to another, the monkey eats all the bananas keptinchThat cell. The monkey enters into the array fromThe upper part and goes outthrough the lower part. Find the maximum number of bananas the monkey can eat. Inputinput starts with an integer T (≤ -), denoting the number of test cases. Every CaseStarts with an integer N (1≤n≤ -). It denotes that, there'll be2*n-1Rows. The ith (1≤i≤n) Line of next N lines contains exactly I numbers. Then there'll be N-1Lines. The jth (1≤j < N) line contains n-j integers. Each number isGreater than zero and less than215. Outputfor each Case, Print the CaseNumber and maximum number of bananas eaten by the monkey. Sample InputOutput forSample Input2476 42 5 Ten9 8 A 22 A 78 2Ten212 31 Case1: the Case2:5
Test instructions from top to bottom, make and Max
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<vector>#include<math.h>#include<queue>#definell Long Long#defineINF 0x3f3f3f3f#defineMet (A, b) memset (A,b,sizeof (a));#defineN 511using namespacestd;intDp[n][n],a[n][n];intMain () {intt,n,con=1; scanf ("%d",&t); while(t--) {scanf ("%d",&N); for(intI=1; i<=n; i++) { for(intj=1; j<=i; J + +) scanf ("%d",&A[i][j]); } for(inti=n+1; i<2*n; i++) { for(intj=1; j<=2*n-i; J + +) scanf ("%d",&A[i][j]); } Met (DP,0); dp[1][1]=a[1][1]; for(intI=2; i<=n; i++) { for(intj=1; j<=i; J + +) {Dp[i][j]=max (dp[i][j],dp[i-1][j-1]+A[i][j]); DP[I][J]=max (dp[i][j],dp[i-1][j]+A[i][j]); } } for(inti=n+1; i<2*n; i++) { for(intj=1; j<=2*n-i; 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", con++,dp[2*n-1][1]); } return 0;}
(Lightoj 1004) Monkey Banana problem simple DP