分析:
代码:
#include<iostream>
#include<cstring>
using namespace std;
const int N = 11;
int f[N * 2][N][N], w[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 * 2; k ++)
{
for(int i1 = 1; i1 <= n; ++i1)
for(int i2 = 1; i2 <= n; ++i2)
{
/*判断j1,j2不能超出边界*/
int j1 = k - i1, j2 = k - i2;
if(j1 < 1 || j1 > n || j2 < 1 || j2 > n)continue;
int &x = f[k][i1][i2];
int t = w[i1][j1]; if(i1 != i2)t += w[i2][j2];
x = max(f[k -1][i1 - 1][i2 - 1], x);
x = max(x, f[k - 1][i1 - 1][i2]);
x = max(x, f[k - 1][i1][i2 - 1]);
x = max(x, f[k - 1][i1][i2]);
x += t;
}
}
cout << f[2 * n][n][n] <<endl;
return 0;
}