题目链接
思路
和这篇博客里的四维数组做法一样点这里传送
转移方程:
[f[x1][y1][x2][y2]=max(f[x1-1][y1][x2-1][y2],f[x1][y1-1][x2][y2-1],f[x1-1][y1][x2][y2-1],f[x1][y1-1][x2-1][y2])+a[x1][y1]
]
注意:
(x1!=x2)而且(y1!=y2)时
[f[x1][y1][x2][y2]+=a[x2][y2]
]
代码:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<math.h>
using namespace std;
int n,x,y,z;
int a[2019][2019];
int f[110][120][119][117];
int main() {
scanf("%d",&n);
scanf("%d%d%d",&x,&y,&z);
while(x&&y&&z) {
a[x][y]=z;
scanf("%d%d%d",&x,&y,&z);
}
for(int x1=1; x1<=n; x1++)
for(int x2=1; x2<=n; x2++)
for(int y1=1; y1<=n; y1++)
for(int y2=1; y2<=n; y2++) {
int t1=max(f[x1-1][y1][x2-1][y2],f[x1][y1-1][x2][y2-1]);
int t2=max(f[x1-1][y1][x2][y2-1],f[x1][y1-1][x2-1][y2]);
f[x1][y1][x2][y2]=max(t1,t2)+a[x1][y1];
if(x1!=x2&&y1!=y2) f[x1][y1][x2][y2]+=a[x2][y2];
}
cout<<f[n][n][n][n];
return 0;
}