You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
1.4 KiB
36 lines
1.4 KiB
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
const int N = 15;
|
|
|
|
int n; // 方格的宽度和高度
|
|
int w[N][N]; // 每个方格里面的数字
|
|
int f[N][N][N][N]; // 四维的DP数组
|
|
|
|
int main() {
|
|
cin >> n;
|
|
// 接下来的每行有三个整数,第一个为行号数,第二个为列号数,第三个为在该行、该列上所放的数。
|
|
int a, b, c;
|
|
// 一行 0 0 0 表示结束。
|
|
while (cin >> a >> b >> c, a || b || c) w[a][b] = c;
|
|
|
|
// 开始递推
|
|
for (int x1 = 1; x1 <= n; x1++)
|
|
for (int y1 = 1; y1 <= n; y1++)
|
|
for (int x2 = 1; x2 <= n; x2++) {
|
|
int y2 = x1 + y1 - x2; // 通过计算可以减少一维循环
|
|
if (y2 >= 1 && y2 <= n) { // 要小心点,别整出一个不符合实际情况的数
|
|
// 上一个状态
|
|
int t = f[x1 - 1][y1][x2 - 1][y2];
|
|
t = max(t, f[x1][y1 - 1][x2][y2 - 1]);
|
|
t = max(t, f[x1 - 1][y1][x2][y2 - 1]);
|
|
t = max(t, f[x1][y1 - 1][x2 - 1][y2]);
|
|
// 加上这个点的数值
|
|
f[x1][y1][x2][y2] = t + w[x1][y1];
|
|
// 如果这个点没有被重复走,那么再加一次
|
|
if (x1 != x2 && y1 != y2) f[x1][y1][x2][y2] += w[x2][y2];
|
|
}
|
|
}
|
|
printf("%d", f[n][n][n][n]);
|
|
return 0;
|
|
} |