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
2
4
7
6 4
2 5 10
9 8 12 2
2 12 7
8 2
10
2
1
2 3
1
Sample Output
Case 1: 63
Case 2: 5
Note
Dataset is huge, use faster I/O methods.
将整个图形2*n-1行分为两部分
上一部分是0~n-1行, a[i][j]=a[i][j]+max(a[i-1][j],a[i-1][j-1]);
下一部分是n~2*n-2行,a[i][j]=a[i][j]+max(a[i-1][j],a[i-1][j+1]);
#include<iostream>
using namespace std;
int a[201][101];
int main() {
int t;
scanf("%d",&t);
for(int k=1; k<=t; k++) {
int n;
scanf("%d",&n);
//输入
for(int i=0; i<n; i++) {
for(int j=0; j<=i; j++) {
scanf("%d",&a[i][j]);
}
}
for(int i=n; i<2*n-1; i++) {
for(int j=0; j<2*n-1-i; j++) {
scanf("%d",&a[i][j]);
}
}
//计算
int maxmum=a[0][0];
for(int i=1; i<n; i++) {
for(int j=0; j<=i; j++) {
if(j==0) a[i][j]=a[i][j]+a[i-1][j];
else {
a[i][j]=a[i][j]+max(a[i-1][j],a[i-1][j-1]);
}
maxmum=max(maxmum,a[i][j]);
}
}
for(int i=n; i<2*n-1; i++) {
for(int j=0; j<2*n-1-i; j++) {
a[i][j]=a[i][j]+max(a[i-1][j],a[i-1][j+1]);
maxmum=max(maxmum,a[i][j]);
}
}
printf("Case %d: %d\n",k,maxmum);
}
return 0;
}