1671: [Usaco2005 dec]knights of Ni Knight time Limit:5 Sec Memory limit:64 MB
submit:254 solved:163
[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.
Source
Silver
The puzzle: Another irrigation method .... Usaco irrigation problem really much = =
But fortunately the number of knights seems to be 1 ... If the Knights were a whole lot more, that would be awful. tt
Or the irrigation method does not explain
1 ConstDirArray[1..4,1..2] ofLongint= ((1,0),(-1,0),(0,1),(0,-1));2 typeArr=Array[0..1000005] ofLongint;3 var4 I,j,k,l,m,n,x0,x1,y0,y1,head,tot,ans:longint;5A,b,h:Array[0..1001,0..1001] ofLongint;6D:Array[0..1000005,1..3] ofLongint;7 C,e:arr;8 functionmin (x,y:longint): Longint;9 beginTen ifX<y ThenMin:=xElsemin:=y; One End; A procedureBFS (X,y:longint;varC:arr); - varF,r,xx,yy,i:longint; - begin thef:=1; r:=2;d [1,1]:=x;d[1,2]:=y;d[1,3]:=0; b[x,y]:=1; - whileF<r Do - begin - fori:=1 to 4 Do + begin -Xx:=d[f,1]+dir[i,1]; +Yy:=d[f,2]+dir[i,2]; A ifb[xx,yy]=1 Thencontinue; atb[xx,yy]:=1; -D[r,3]:=d[f,3]+1; -D[r,1]:=xx;d[r,2]:=yy; - ifH[xx,yy]>0 Then - begin -C[h[xx,yy]]:=d[r,3]; in End; - Inc (R); to End; + Inc (f); - End; the End; * begin $randomize;tot:=0;Panax Notoginseng readln (m,n); - fori:=0 ton+1 Do the begin +a[i,m+1]:=1; AA[i,0]:=1; the End; + fori:=0 tom+1 Do - begin $a[n+1, i]:=1; $a[0, i]:=1; - End; -Fillchar (H,sizeof (h),0); the fori:=1 toN Do - forj:=1 toM DoWuyi begin the read (a[i,j]); - CaseA[I,J] of Wu 2:begin -x0:=i;y0:=J; About End; $ 3:begin -x1:=i;y1:=J; -a[i,j]:=1; - End; A 4:begin + Inc (TOT); theh[i,j]:=tot; -a[i,j]:=0; $ End; the End; the if((jMoD +)=0)or(j=m) ThenReadln; the End; the fori:=0 ton+1 Do - forj:=0 tom+1 Do inb[i,j]:=A[i,j]; theFillchar (C,sizeof (c),0); the BFS (x0,y0,c); About fori:=0 ton+1 Do the forj:=0 tom+1 Do theb[i,j]:=A[i,j]; theFillchar (E,sizeof (E),0); + BFS (x1,y1,e); -ans:=Maxlongint; the fori:=1 toTot DoBayi begin the if(c[i]=0)or(e[i]=0) Thencontinue; theAns:=min (ans, (e[i]+C[i])) - End; - writeln (ans); the Readln; the End.
1671: [Usaco2005 dec]knights of Ni Knight