Description
After a hard journey, the protagonist's little p rides back in the airship. On the way back, small p found in the rambling desert, there is a narrow green space is particularly conspicuous. Look down carefully, only to find this is a playground, designed for the tired people on the road. The casino can be seen as an area of nxm size, and the NXM area is divided into nxm small squares, each with an entertaining item. However, little p did not like all of the entertainment items, so he gave each project a satisfaction level. Satisfaction is positive when the small P likes this item, the larger the value the more like it. Negative means he doesn't like it, the greater the absolute value of the negative number means the less he likes it. When he was 0, he was not fond of the project. Little P decided to park the airship in a small lattice, and then each step he could move to a grid of four squares adjacent to each other. Small p wants to find a path, starting from the airship, and finally back to the grid. Little P has a habit, never like wasting time. So he wants to go through every lattice that makes sense: when he's in a place, he must feel the thrills and thrills of the following, whether he likes the entertainment there or not. Moreover, in addition to the airship in the grid, the other lattice he would not want to pass two times. Little P wants to go through at least four squares of his own. In the case of satisfying these conditions, little p wants to have the highest degree of satisfaction with the entertainment items he has played. Can you help him find the highest degree of satisfaction?
Input
The first behavior in the input file is two positive integers n and m, indicating that the playground size is NXM. Because this casino is very narrow, so N and M meet: 2<=n<=100,2<=m<=6. In the next n rows, each line has m integers, and row i, Column J, indicates the satisfaction of the amusement item in the small lattice of the amusement park in row I, Column J, and this satisfaction range is [ -1000,1000]. Two integers in the same row are separated by a space.
Output
Only one behavior in the output file is an integer representing the highest degree of satisfaction.
Sample Input4 4
100 300-400 400
-100 1000 1000 1000
-100-100-100-100
-100-100-100Sample Output4000
Bare plug DP
#include <queue>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intinf=2e9;Const intmaxn=2187;inti;structna{intx,z; Na (intXxintZZ): X (XX), Z (ZZ) {}};intn,m,x,y,z,a[ +],k,p1,p2,en,t;BOOLmap[ One][101];intf[2][maxn+1],ans=-inf,re[101][ One];intv[2][maxn+1];queue<na>Q;inlineintGxintXintQ1,intQ2) {k=0; for(Registerinti=m+1; i;i--) k=k*3+ (i==x?q1: (i==x+1? Q2:a[i]));returnK;} InlinevoidUpintXintZintLjintLA) { if(LA) lj+=re[x/m+1][x%m+1]; X++; K=x%2; if(v[k][z]!=x) v[k][z]=x,f[k][z]=-Inf,q.push (Na (x,z)); if(F[K][Z]<LJ) f[k][z]=LJ;}intMain () {registerinti,j,p; scanf ("%d%d",&n,&m); memset (F,inf,sizeof(f)); for(j=1; j<=n;j++) for(i=1; i<=m;i++)-scanf ("%d", &re[j][i]), map[i][j]=1; En=n*m-1; f[0][0]=0; v[0][0]=1; Q.push (Na (0,0)); while(!Q.empty ()) {P=0; Na no=Q.front (); Q.pop (); intan=f[no.x%2][no.z]; if(no.x%m==0) no.z*=3; X=no.x%m+1; y=no.x/m+1; for(i=1; i<=m+1; i++) a[i]=0; for(i=1, j=no.z;j;i++,j/=3) {A[i]=j%3; if(A[i]) p++; } if(a[x]==1&&a[x+1]==2){ if(ans<an+re[y][x]&&p==2) ans=an+Re[y][x]; }Else if(a[x]==2&&a[x+1]==1) Up (NO.X,GX (x,0,0), AN,1);Else if(a[x]==0&&a[x+1]==0){ if(no.x!=en) { if(map[x][y+1]&&map[x+1][y]) Up (NO.X,GX (x,1,2), AN,1); Up (NO.X,GX (x,0,0), AN,0); } }Else if(a[x]==0){ if(map[x+1][y]) Up (NO.X,GX (x,0, a[x+1]), AN,1); if(map[x][y+1]) Up (NO.X,GX (x,a[x+1],0), AN,1); }Else if(a[x+1]==0){ if(map[x+1][y]) Up (NO.X,GX (x,0, A[x]), an,1); if(map[x][y+1] Up (NO.X,GX (x,a[x),0), AN,1); }Else if(a[x]==a[x+1]) {P1=p2=0; if(a[x]==1) for(j=0, i=x+2; i<=m+1; i++){ if(a[i]==1) j--; if(a[i]==2) J + +; if(j>0&&!P1) p1=i,j--; if(j>0&&P1) {p2=i; Break;} }Else for(j=0, i=x-1; i;i--){ if(a[i]==1) J + +; if(a[i]==2) j--; if(j>0&&!P2) p2=i,j--; if(j>0&&P2) {p1=i; Break;} } A[P1]=1; a[p2]=2; Up (NO.X,GX (x,0,0), AN,1); }} printf ("%d\n", ans);}
bzoj:1187: [HNOI2007] Magical Amusement Park