参考:
AcWing 275. 证明传纸条为何可以使用方格取数的代码
AcWing 275. 传纸条
#include <iostream>
using namespace std;
const int N = 51;
int m, n;
int x, y, w;
int g[N][N];
int f[N*2][N][N];
const int N = 51;
int m, n;
int x, y, w;
int g[N][N];
int f[N*2][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<=n+m; k++)
for(int j=1; j<=n; j++)
for(int b=1; b<=n; b++)
{
int i = k-j, a = k-b;
if(i>=1 && i<=m && a>=1 && a<=m)
{
f[k][j][b] = max(max(f[k-1][j][b], f[k-1][j-1][b]),
max(f[k-1][j][b-1], f[k-1][j-1][b-1]));
if(j == b)
f[k][j][b] += g[i][j];
else
f[k][j][b] += (g[i][j] + g[a][b]);
}
}
cout << f[n+m][n][n];
}