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 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#include<bits/stdc++.h> using namespace std;
#define LL long long #define PII pair<int,int> #define PLL pair<LL,LL> #define MP make_pair #define F first #define S second #define INF INT_MAX
inline void optimizeIO() { ios_base::sync_with_stdio(false); cin.tie(NULL); }
const int nmax = 1e2+7; const LL LINF = 1e17;
string to_str(LL x) { stringstream ss; ss<<x; return ss.str(); }
int n,m; LL ara[nmax][nmax];
LL dp[nmax][nmax][nmax][3];
LL solve(int row,int lc,int rc,int move) { if(row==n-1 && lc==m-2 && rc==m-1 && move==0) return 0;
LL &ret = dp[row][lc][rc][move];
if(ret!=-1) return ret;
ret = 0;
if(move==0) { if(lc+1<rc) ret = max(ret, solve(row,lc+1,rc,0) + ara[row][lc+1]); ret = max(ret, solve(row,lc,rc,1)); } else if(move==1) { if(rc+1<m) ret = max(ret, solve(row,lc,rc+1,1) + ara[row][rc+1]); if(rc>lc) ret = max(ret, solve(row,lc,rc,2)); } else { if(lc<rc && row+1<n) ret = max(ret, solve(row+1,lc,rc,0) + ara[row+1][lc] + ara[row+1][rc]); }
return ret;
}
int main() { int tc; cin>>tc;
for(int q=1; q<=tc; q++) { cin>>n>>m;
for(int i=0; i<n; i++) for(int j=0; j<m; j++) cin>>ara[i][j];
memset(dp,-1,sizeof dp);
LL ans = solve(0,0,0,1) + ara[0][0];
cout<<"Case "<<q<<": "<<ans<<endl;
}
return 0; }
|