AcWing 275. 传纸条
原题链接
中等
作者:
总有刁民想害朕
,
2020-03-09 19:11:10
,
所有人可见
,
阅读 611
C++ 代码
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 55;
int g[N][N];
int dp[N+N][N][N];
int n, m;
int main(){
cin >> n >> m;
for(int i = 1;i <= n;++i)
for(int j = 1;j <= m;++j)
cin >> g[i][j];
for(int k = 2;k <= n + m;++k)
for(int i1 = 1;i1 <= n;++i1)
for(int i2 = 1;i2 <= n;++i2){
int j1 = k - i1, j2 = k - i2;
if(j1 >= 1 && j1 <= m && j2 >= 1 && j2 <= m){
int w = (i1 == i2 ? g[i1][j1] : g[i1][j1] + g[i2][j2]);
int &t = dp[k][i1][i2];
t = max(t, dp[k-1][i1][i2] + w);
t = max(t, dp[k-1][i1-1][i2] + w);
t = max(t, dp[k-1][i1][i2-1] + w);
t = max(t, dp[k-1][i1-1][i2-1] + w);
}
}
cout << dp[n+m][n][n] << endl;
return 0;
}