Description
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 cellsdown 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.
Output
For each case, print the case number and maximum number of bananas eaten by the monkey.
Sample Input
Sample Output
#include#include #define N 1005#define MAX(a,b) ((a)>(b)?(a):(b))int a[2][N];int main(){ int t,n,i,j,id,kase=0; scanf("%d",&t); while(t--) { memset(a,0,sizeof(a)); scanf("%d",&n); id=0; for(i=1;i<=n;i++) { for(j=1;j<=i;j++) { scanf("%d",&a[id][j]); } for(j=1;j<=i;j++) { a[id][j]+=MAX(a[id^1][j-1],a[id^1][j]); } id^=1; } for(i=n-1;i>0;i--) { for(j=1;j<=i;j++) { scanf("%d",&a[id][j]); } for(j=1;j<=i;j++) { a[id][j]+=MAX(a[id^1][j],a[id^1][j+1]); } id^=1; } printf("Case %d: %d\n",++kase,a[id^1][1]); } return 0;}