Topic Portal: Http://www.nocow.cn/index.php/Translate:USACO/camelot
This problem makes me heartache, a start to write not, later dragged for a long time, decided to see the puzzle, did not expect to read the official problem, alas! Later saw the following to do out. In the case of the answer, it is probably the first preprocessing of each lattice to a different lattice position, and then enumerated in the king's coordinates +-2 position to connect the king, enumerate all the pieces of the concentration points added up to calculate the good. My code is slightly rude for convenience.
Routing door: Http://www.nocow.cn/index.php/USACO/camelot
/*Id:abc31261lang:c++task:camelot*/#include<cstdio>#include<cstring>#include<queue>#include<iostream>using namespacestd;Const intmaxn= -, maxm= -;intn,m,ans=100000000, flag[maxn][maxn],f1[2][maxn][maxn][maxn][maxn],f[maxn][maxn],num=-1, numx[maxm],numy[maxm],knx[]={0,-2,-2,-1,-1,1,1,2,2},kny[]={0,-1,1,-2,2,-2,2,-1,1}, ky[]={0,-1,0,1,-1,1,-1,0,1},kx[]={0,-1,-1,-1,0,0,1,1,1};voidRead () {Char inch[5]; scanf ("%d%d",&n,&m); while(SCANF ("%s%d",inch, &numx[++num]) = =2) numy[num]=inch[0]-'A'+1; Num--;}BOOLCheckintXinty) { if(X<=n && x>0&& y<=m && y>0)return true; return false;}voidBFsintAintBintc) {Queue<int>qx,qy; Qx.push (a), Qy.push (b); memset (Flag,true,sizeof(flag)); FLAG[A][B]=false; F1[C][A][B][A][B]=0; while(!Qx.empty ()) { intX=qx.front (), y=Qy.front (); Qx.pop (), Qy.pop (); for(intI=1; i<=8; i++) { intx1=x+knx[i],y1=y+Kny[i]; if(c==1) x1=x+kx[i],y1=y+Ky[i]; if(Check (x1,y1) &&Flag[x1][y1]) {Qx.push (x1), Qy.push (y1); FLAG[X1][Y1]=false; F1[C][A][B][X1][Y1]=f1[c][a][b][x][y]+1; } } }}voidsolve () {intq,i,j,l,k; for(i=1; i<=n;i++) for(j=1; j<=m;j++) {F[i][j]+=f1[1][numx[0]][numy[0]][i][j]; Ans=min (ans,f[i][j]); } for(q=1; q<=num;q++) for(i=1; i<=n;i++) for(j=1; j<=m;j++) for(l=numx[0]-2; l<=numx[0]+2; l++) for(k=numy[0]-2; k<=numy[0]+2; k++) if(l>0&& l<=n && k>0&& k<=m) ans=min (f1[1][numx[0]][numy[0]][l][k]+f1[0][numx[q]][numy[q]][l][k]+f1[0][l][k][i][j]+f[i][j]-f1[0][numx[q]][numy[q]][i][j]-f1[1][numx[0]][numy[0]][i][j],ans); }intMain () {inti,j,l; Freopen ("camelot.in","R", stdin); Freopen ("Camelot.out","W", stdout); Read (); Memset (F,0,sizeof(f)); memset (F1,0x2f,sizeof(F1)); BFS (numx[0],numy[0],1); for(i=1; i<=n;i++) for(j=1; j<=m;j++) BFS (I,j,0); for(i=1; i<=num;i++) for(j=1; j<=n;j++) for(l=1; l<=m;l++) F[j][l]+=f1[0][numx[i]][numy[i]][j][l]; Solve (); cout<<ans<<Endl; return 0;}
[Usaco3.3.3]camelot