Files
python/TangDou/AcWing/NumberTriangles/1027_0.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

34 lines
1.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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++)
for (int y2 = 1; y2 <= n; y2++) {
if (x1 + y1 == x2 + y2) {
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];
// 如果这个点没有被重复走,那么再加一次w(x2,y2)
if (x1 != x2 && y1 != y2) f[x1][y1][x2][y2] += w[x2][y2];
}
}
printf("%d", f[n][n][n][n]);
return 0;
}