Evaluation Portalinput
3 6 ( 5) 4 145 2 215 6 3 157 2 0 0
output
67
Practice:
It's like a note, but it's a little different.
The most basic approach, open four-dimensional violence DP, can update the update
We can also save space time by updating the state of each layer and using the Manhattan distance to represent another dimension in one dimension.
Code "1"
#include <stdio.h>#include<algorithm>using namespacestd;intmp[ One][ One],f[ One][ One][ One][ One];intMain () {intN,x,y,c; scanf ("%d",&N); while(SCANF ("%d%d%d", &x,&y,&c) && x && y && c) mp[x][y]=C; for(intI=1; i<=n;++i) for(intj=1; j<=n;++j) for(intk=1; k<=n;++k) {intl=i+j-K; if(l<=0) Break; F[I][J][K][L]=max (f[i-1][j][k-1][l],max (f[i-1][j][k][l-1],max (f[i][j-1][k-1][l],f[i][j-1][k][l-1]))); if(i==k && j==l) f[i][j][k][l]+=Mp[i][j]; Elsef[i][j][k][l]+=mp[i][j]+Mp[k][l]; } printf ("%d", F[n][n][n][n]); return 0;}
Code "2"
#include <stdio.h>#include<algorithm>using namespacestd;intmp[ One][ One],f[ One][ One];intMain () {intN,x,y,c; scanf ("%d",&N); while(SCANF ("%d%d%d", &x,&y,&c) && x && y && c) mp[x][y]=C; for(intL=2; l<=n+n;++l) for(inti=l-1; i>=1;--i) for(intii=l-1; ii>=1;--II) {intj=l-i,jj=l-II; F[I][II]=max (F[i][ii],max (f[i-1][ii-1],max (f[i-1][ii],f[i][ii-1])))+Mp[i][j]; if(i!=II) {F[I][II]+=MP[II][JJ]; }} printf ("%d", F[n][n]); return 0;}
Number of squares (Luogu 1004)