-
Notifications
You must be signed in to change notification settings - Fork 0
/
loj1071.cpp
81 lines (57 loc) · 1.25 KB
/
loj1071.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// UNSOLVED........:(
using namespace std;
#include<iostream>
#include<cstdio>
#include<cstring>
#define max(a,b) ((a>b)?(a):(b))
int dp[102][102],arr[102][102],m,n;
bool visited[102][102];
int solve1(int i,int j)
{
if(i==m&&j==n)
{
return arr[i][j];
}
if(visited[i][j]==true)
return dp[i][j];
int r1=0,r2=0;
if(i+1<=m)
r1=arr[i][j]+solve1(i+1,j);
if(j+1<=n)
r2=arr[i][j]+solve1(i,j+1);
visited[i][j]=true;
return dp[i][j]=max(r1,r2);
}
int solve2(int i,int j)
{
if((i==1&&j==1)||(visited[i][j]==true))
{
return arr[i][j];
}
if(dp[i][j]!=-1)
return dp[i][j];
int r1=0,r2=0;
if(i-1>=1)
r1=arr[i][j]+solve2(i-1,j);
if(j-1>=1)
r2=arr[i][j]+solve2(i,j-1);
return dp[i][j]=min(r1,r2);
}
int main()
{
int T,Case=1;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&n);
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
scanf("%d",&arr[i][j]);
memset(visited,false,sizeof(visited));
int ans1=solve1(1,1);
memset(dp,-1,sizeof(dp));
int ans2=solve2(m,n);
//cout<<ans1<<" "<<ans2<<endl;
printf("Case %d: %d\n",Case++,ans1+ans2+1);
}
}