-
Notifications
You must be signed in to change notification settings - Fork 0
/
1004.cpp
64 lines (58 loc) · 1.13 KB
/
1004.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <stdio.h>
#include <string.h>
#define siz 210
using namespace std;
int ar[siz][siz],dp[siz][siz];
int DP(int r,int c)
{
if(dp[r][c]!=(-1))
return dp[r][c];
int a,b;
a=DP(r+1,c);
b=DP(r+1,c+1);
dp[r][c]=max(a,b)+ar[r][c];
return dp[r][c];
}
int main()
{
int t,tc,n,i,j,cl,R,ans;
scanf("%d",&t);
tc=0;
while(tc<t)
{
tc++;
scanf("%d",&n);
memset(ar,0,sizeof(ar));
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
scanf("%d",&ar[i][j]);
}
R=2*n-1;
cl=0;
for(i=n;i<R;i++)
{
cl++;
for(j=0;j<n;j++)
{
if((j+cl)==n)
break;
scanf("%d",&ar[i][j+cl]);
}
}
memset(dp,-1,sizeof(dp));
for(i=0;i<=R;i++)
{
for(j=0;j<=n;j++)
{
if(ar[i][j]==0)
dp[i][j]=0;
}
}
dp[R-1][n-1]=ar[R-1][n-1];
ans=DP(0,0);
printf("Case %d: %d\n",tc,ans);
}
return 0;
}