AcWing 275. 传纸条
原题链接
中等
### 滚动数组优化空间
#include <iostream>
#include <cstring>
using namespace std;
const int N = 55;
int n, m;
int g[N][N], f[N][N], f1[N][N];
int main()
{
cin >> m >> n;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
cin >> g[i][j];
}
}
for (int k = 2; k <= m + n; k++) {
memcpy(f1, f, sizeof f);
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= m; j++) {
if (k - i >= 1 && k - i <= n && k - j >= 1 && k - j <= n) {
int t = g[i][k - i];
if (i != j) t += g[j][k - j];
int &x = f[i][j];
x = f1[i - 1][j - 1] + t;
x = max(x, f1[i - 1][j] + t);
x = max(x, f1[i][j - 1] + t);
x = max(x, f1[i][j] + t);
}
}
}
}
cout << f[m][m] << endl;
return 0;
}