[Usaco2005 Dec] Knights of Ni Knight description Bessie encountered a very troublesome thing: she stumbled into a castle in the forest, and if she wanted to go home, she had to walk through the forest guarded by the Knights. To be able to leave safely, Bessie had to look for a special shrub in the forest and bring a tree to them, as the Knights demanded. Of course, Bessie wanted to leave the dreadful forest early, so she had to finish the task of the Knights as soon as possible, and Bessie carried the map of the forest, which was put into a Cartesian coordinate system, and divided into WxH (1≤w,h≤1000) blocks on the x, Y axis of the unit length. Bessie identified herself and the Knights on the map, and of course the map marked the area where she needed the bushes to grow. Some areas cannot be passed (such as swamps, cliffs, and human-eating rabbits). Before the Bushes were found, Bessie could not pass through the area where the Knights were, in order to make sure she would not get lost, Bessie moved only four directions north, east, south, due west (note that she would not walk diagonally). She had to walk a whole day to get from a certain area to the area adjacent to it. input data to ensure that Bessie is bound to complete the Knight's task. Betsy wants you to help her figure out how many geniuses she needs at least to get out of this horrible place? input line 1th Enter 2 integers separated by spaces, that is, the W, h. mentioned in the title, and then enter the map that Bessie holds. Each row uses several numbers to represent the terrain of the corresponding row on the map. The 1th row describes the northernmost row of land in the map, and the last line describes the most southerly. Adjacent numbers correspond to regions that are adjacent. If the width of the map is less than or equal to 40, then each row of numbers corresponds exactly to the row of land on the map. If the map is larger than 40, then each row will only give 40 numbers, and ensure that each row in addition to the last line contains exactly 40 digits. There are no lines in the description of the area distributed in two different rows. map of the terrain corresponding to the numbers: 0: Bessie can pass through the open space 1: Inaccessible areas due to various reasons 2: Betsy's position now 3: the Knights ' location 4: The land output of the shrubs which Bessie needed Outputs a positive integer d, that is, how many days it will take for Bessie to finish the task given by the Knights. Sample INPUT8 4
4 1 0 0 0 0 1 0
0 0 0 1 0 1 0 0
0 2 1 1 3 0 4 0
0 0 0 4 1 1 1 0
INPUT DETAILS:
Width=8, height=4. Bessie starts on the third row, only a few squares away
From the Knights.
Sample Output11
HINT
The length of the forest is 8 and the width is 4. Bessie's starting position was in line 3rd, not far from the Knights.
Bessie could complete the task of the Knights on such a route: North, West, north, south, east, east, north, east, east, south, south. She gets a bush in the northwest corner of the forest that she needs, and then bypasses the barrier and gives it to the Knights in the south-east.
Solving: Double BFS, brush water is good for health.
#include <cstdio>#include<cstring>#include<iostream>#include<queue>#defineOk (A, b) (a>=1&&a<=w&&b>=1&&b<=h&&map[a][b]!=1)using namespacestd;intmap[1010][1010];intw,h,ans,dis[1010][1010][2],inq[1010][1010],x1,x2,y1,y2;intdir[][2]={{-1,0},{1,0},{0,1},{0,-1}};queue<int>qx,qy;intReadin () {intret=0;CharGC; while(gc<'0'|| Gc>'9') gc=GetChar (); while(gc>='0'&&gc<='9') ret=ret*Ten+gc-'0', gc=GetChar (); returnret;}voidBFsintXintYintt) {Dis[x][y][t]=0; Qx.push (x), Qy.push (y); intI,tx,ty; while(!Qx.empty ()) {x=qx.front (), y=Qy.front (); Qx.pop (), Qy.pop (); for(i=0;i<4; i++) {TX=x+dir[i][0],ty=y+dir[i][1]; if(OK (tx,ty) &&dis[tx][ty][t]>dis[x][y][t]+1) {Dis[tx][ty][t]=dis[x][y][t]+1; if(!Inq[tx][ty]) {Inq[tx][ty]=1; Qx.push (TX), Qy.push (Ty); }}} Inq[x][y]=0; }}intMain () {h=readin (), w=Readin (); inti,j; for(i=1; i<=w;i++) { for(j=1; j<=h;j++) {Map[i][j]=Readin (); if(map[i][j]==2) x1=i,y1=J; if(map[i][j]==3) x2=i,y2=J; }} memset (Dis,0x3f,sizeof(DIS)); BFS (X1,y1,0), BFS (X2,y2,1); Ans=1<< -; for(i=1; i<=w;i++) for(j=1; j<=h;j++) if(map[i][j]==4) ans=min (ans,dis[i][j][0]+dis[i][j][1]); printf ("%d", ans); return 0;}
"BZOJ1671" [Usaco2005 dec]knights of Ni Knight BFS