lightoj 1036 dp
题目链接:http://lightoj.com/volume_showproblem.php?problem=1036
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <cstdio> #include <cstring> #include <iostream> #include <cmath> #include <algorithm> #include <queue> #include <vector> using namespace std; const int maxn = 505; int m,n; int dp[maxn][maxn][2]; int leftsum[maxn][maxn],upsum[maxn][maxn]; int main() { //freopen("E:\\acm\\input.txt","r",stdin); int T; cin>>T; for(int cas=1;cas<=T;cas++){ cin>>m>>n; for(int i=1;i<=m;i++) upsum[0][i] = 0; for(int i=1;i<=n;i++) leftsum[i][0] = 0; for(int i=1;i<=m;i++) for(int j=1;j<=n;j++){ int a; scanf("%d",&a); leftsum[i][j] = leftsum[i][j-1] + a; } for(int i=1;i<=m;i++) for(int j=1;j<=n;j++){ int a; scanf("%d",&a); upsum[i][j] = upsum[i-1][j] + a; } memset(dp,0,sizeof(dp)); for(int sum=2;sum<=m+n;sum++){ for(int i=1;i<=sum-1;i++){ int j = sum - i; if(i>m || j>n) continue; dp[i][j][1] = max(dp[i][j-1][1],dp[i][j-1][0]) + upsum[i][j]; dp[i][j][0] = max(dp[i-1][j][1],dp[i-1][j][0]) + leftsum[i][j]; } } int ans = max(dp[m][n][1],dp[m][n][0]); printf("Case %d: %d\n",cas,ans); } }