某人从图中的左上角的A出发,可以向下行走,也可以向右行走,直到达右下角的B点。在走过的路上,他可以取走方格中的数(取走后的方格中将变为数字0)。
此人从A点到B点共走了两次,试找出两条这样的路径,使得取得的数字和为最大。
INPUT
8
2 3 13
2 6 6
3 5 7
4 4 14
5 2 21
5 6 4
6 3 15
7 2 14
0 0 0
OUTPUT
67
#include<iostream>
using namespace std;
const int maxn=101;
int a[maxn][maxn],f[maxn*2][maxn][maxn];
int n,m;
int main(){
cin>>n;
int x=1,y,w;
while (1){
cin>>x>>y>>w;
a[x][y]=w;
if (x==0&&y==0&&w==0) break;
}
for (int l=2;l<=2*n;l++) //阶段
for (int x1=1;x1<=n;x1++)
for (int x2=1;x2<=n;x2++){
int y1=l-x1;
int y2=l-x2;
if (y1<=0) continue;
if (y2<=0) continue;
int s=f[l][x1][x2];
s=max(s,f[l-1][x1-1][x2-1]);
s=max(s,f[l-1][x1][x2-1]);
s=max(s,f[l-1][x1-1][x2]);
s=max(s,f[l-1][x1][x2]);
f[l][x1][x2]+=s;
if (x1==x2&&y1==y2) f[l][x1][x2]+=a[x1][y1]; //重合
else f[l][x1][x2]+=a[x1][y1]+a[x2][y2];
}
cout<<f[2*n][n][n];
}