方格取数
作者:
恒_41
,
2024-04-19 12:53:34
,
所有人可见
,
阅读 2
//这里填你的代码^^
#include<stdio.h>
#include<iostream>
using namespace std;
const int N = 15;
int w[N][N];
int f[N + N][N][N];
int main() {
int n;//边长
cin >> 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 = 1; i1 <= n; i1++)
for (int i2 = 1; i2 <= n; i2++)
{
int j1 = k - i1;
int j2 = k - i2;//求出纵坐标
if (j1 >= 1 && j1 <= n && j2 >= 1 && j2 <= n) {
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][i2 - 1]+t);//右下
x = max(x, f[k - 1][i1 - 1][i2]+t);//下右
x = max(x, f[k - 1][i1][i2]+t);//右右
}
}
cout << f[2 * n][n][n];
return 0;
}
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~