我觉得这题跟 方格取数 这题没差
但我调半天都挑不出来气死了气死了气死了。
一定搞清楚各种定义!!!
比如:f[k][i1][i2]
,$i1, i2$ 分别是是两个点的 $x$ 坐标,$k$ 应该是步数吧
所以最后输出的时候也应该写 f[n + m][n][n]
,而不是f[n + m][n][m]
(这个错怎么也找不出来,看来我应该锻炼人肉debug能力,并且要静下心来去想题,去想每一行是什么意思)
冷静思考!
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 60;
int n, m;
int w[N][N];
int f[2 * N][N][N];
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= m; j ++ )
scanf("%d", &w[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 t = w[i1][j1];
if (i1 != i2) t += w[i2][j2];
int &x = f[k][i1][i2];
x = max(x, f[k - 1][i1 - 1][i2 - 1] + t);
x = max(x, f[k - 1][i1 - 1][i2] + t);
x = max(x, f[k - 1][i1][i2 - 1] + t);
x = max(x, f[k - 1][i1][i2] + t);
}
}
printf("%d\n", f[n + m][n][n]);
return 0;
}
刚开始就觉得这俩题一样,于是抄上之后怎么也改不对,于是…
看了别人的写法,写暴力写出来了hh还挺爽
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 60;
int n, m;
int w[N][N];
int f[N][N][N][N];
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= m; j ++ )
scanf("%d", &w[i][j]);
for (int i1 = 1; i1 <= n ; i1 ++ )
for (int j1 = 1; j1 <= m ; j1 ++ )
for (int i2 = 1; i2 <= n ; i2 ++ )
for (int j2 = 1; j2 <= m ; j2 ++ )
{
int x = w[i1][j1] + w[i2][j2];
int a = max(f[i1-1][j1][i2-1][j2], f[i1-1][j1][i2][j2-1]); // 下下 下右
int b = max(f[i1][j1-1][i2-1][j2], f[i1][j1-1][i2][j2-1]); // 右下 右右
f[i1][j1][i2][j2] = max(a, b) + x;
if (i1 == i2 && j1 == j2) f[i1][j1][i2][j2] -= w[i1][j1];
}
printf("%d\n", f[n][m][n][m]);
return 0;
}