题目不难,就是各种边界处理要考虑一下
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int f[101][51][51], map[51][51], n, m;
int main()
{
cin >> m >> n;
int i, j, k;
for (i = 1; i <= m; i++)
for (j = 1; j <= n; j++)
cin >> map[i][j];
f[2][1][1] = map[1][1];//记得初始化
for(i=3;i<=n+m;i++)
for(j=1;j<i&&j<=m;j++)
for (k = 1; k < i&&k <= m; k++) {
f[i][j][k] = max(max(f[i - 1][j][k], f[i - 1][j][k - 1]), max(f[i - 1][j - 1][k], f[i - 1][j - 1][k - 1]));
//不用管出界的情况,因为出界就是0,肯定不会是最大值
if (j == k)f[i][j][k] += map[j][i - j];//判重
else f[i][j][k] += map[j][i - j] + map[k][i - k];
}
cout << f[n + m][m][m] << endl;
return 0;
}