1671: [Usaco2005 dec]knights of Ni Knight time Limit:5 Sec Memory limit:64 MB
submit:281 solved:180
[Submit] [Status] [Discuss] Description
Bessie are in Camelot and have encountered a sticky situation:she needs to pass through the forest that's guarded by the K Nights of Ni. In order to pass through safely, the Knights has demanded that she bring them a single shrubbery. Time is of the essence, and Bessie must find and bring them a shrubbery as quickly as possible. Bessie have a map of the forest, which is partitioned into a square grid arrayed in the usual manner, with axes parallel To the X and Y axes. The map is a W x H units in size (1 <= W <=; 1 <= H <= 1000). The map shows where Bessie starts her quest, the single square where the Knights of Ni is, and the locations of all the S Hrubberies of the land. It also shows which areas of the map can be traverse (some grid blocks is impassable because of swamps, cliffs, and Kille R rabbits). Bessie can not pass through the Knights of Ni Square without a shrubbery. In order to make sure this she follows the map correctly, Bessie can only move in four DirectiOns:north, East, South, or West (i.e, not diagonally). She requires a traversal from one grid block to a neighboring grid block. It's guaranteed that Bessie'll be able to obtain a shrubbery and then deliver it to the Knights of Ni. Determine the quickest-a-to-do.
Bessie had a very troublesome affair: she accidentally broke 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 of the shrubs needed by Bessie output output a positive integer d, that is, how long 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.
A SPFA from the start and end points, recording each shrub's distance to each bush, then enumerating each shrub position, seeking the minimum distance from the start and end points, and
1 /*by Silvern*/2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cstdio>6#include <cmath>7#include <queue>8 using namespacestd;9 Const intmxn= +;Ten intw,h; One intSx1,sy1,sx2,sy2,tx,ty; A inttar[mxn][3],cnt; - intMP[MXN][MXN]; - intmx[5]={0,1,0,-1,0}, themy[5]={0,0,1,0,-1}; - intdis[mxn][mxn][2]; - BOOLVIS[MXN][MXN]; -queue<pair<int,int> >Q; + voidBFS (intSxintSyintmode) { -dis[sx][sy][mode]=0; +vis[sx][sy]=1; AQ.push (pair<int,int>(Sx,sy)); at while(!Q.empty ()) { - intX=q.front (). first,y=Q.front (). Second; - Q.pop (); - for(intI=1; i<=4; i++){ - intnx=x+mx[i],ny=y+My[i]; - if(nx>0&& nx<=h && ny>0&& ny<=W) in if(!vis[nx][ny] && (mp[nx][ny]==0|| mp[nx][ny]==4)){ -dis[nx][ny][mode]=dis[x][y][mode]+1; tovis[nx][ny]=1; +Q.push (pair<int,int>(Nx,ny)); - } the } * } $ }Panax Notoginseng intMain () { -scanf"%d%d",&w,&h); the inti,j; + for(i=1; i<=h;i++) A for(j=1; j<=w;j++){ thescanf"%d",&mp[i][j]); + if(mp[i][j]==2) sx1=i,sy1=J; - if(mp[i][j]==3) sx2=i,sy2=J; $ if(mp[i][j]==4) tar[++cnt][1]=i,tar[cnt][2]=J; $ } -BFS (Sx1,sy1,0); -memset (Vis,0,sizeofvis); theBFS (Sx2,sy2,1); - intans=0x5fffff;Wuyi for(i=1; i<=cnt;i++){ the if(dis[tar[i][1]][tar[i][2]][1]!=0&& dis[tar[i][1]][tar[i][2]][0]!=0) -Ans=min (ans,dis[tar[i][1]][tar[i][2]][1]+dis[tar[i][1]][tar[i][2]][0]); Wu } -printf"%d\n", ans); About return 0; $}
POJ3170 Bzoj1671 [Usaco2005 dec]knights of Ni Knight