AcWing 1027. 方格取数
原题链接
简单
作者:
偷月亮的喵
,
2024-12-16 14:25:27
,
所有人可见
,
阅读 1
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 15;
int n;
int w[N][N];
int f[N * 2][N][N];
int main()
{
scanf("%d", &n);
int a, b, c;
while (cin >> a >> b >> c, a || b || c) w[a][b] = c;
for (int k = 2; k <= n + n; k ++ )
for (int i1 = max(1, i1 - n); i1 <= n && i1 < k; i1 ++ )
for (int i2 = max(1, i1 - n); i2 <= n && i2 < k; i2 ++ )
{
int j1 = k - i1, j2 = k - i2;
int &x = f[k][i1][i2];
if(i1 != i2 || k == 2 || k == n + n){
int t = w[i1][j1];
if(i1 != i2)
t += w[i2][j2];
for(int g = 0; g <= 1; g ++ ){
for(int h = 0; h <= 1; h ++ ){
x = max(x, f[k - 1][i1 - g][i2 - h] + t);
}
}
}
}
printf("%d\n", f[n + n][n][n]);
return 0;
}