1632: [Usaco2007 feb]lilypad Pond time limit:5 Sec Memory limit:64 MB
submit:404 solved:118
[Submit] [Status] [Discuss] Description
Farmer John built a beautiful pond for his cows to appreciate and exercise. The rectangular pool is divided into M-rows and N-columns (1≤m≤30; 1≤n≤30) square squares. Some of the squares have amazing sturdy lotus flowers, some rocks, and the rest are just beautiful, pure, blue water. Bessie was practicing ballet, and she jumped from one Lotus to another, presently in a Lotus. She wants a jump on the lotus One, and the target is another given Lotus. She could jump neither into the water nor to a rock. To the layman's surprise, Bessie's jumps were like Chinese chess horses: moving horizontally 1, moving vertically 2, or moving vertically 1, moving horizontally 2. Bessie may sometimes have up to 8 jumps of choice. Farmer John, observing Bessie's ballet connection, realized that sometimes Bessie might not be able to jump where she wanted to go because there were no Lotus flowers on the road. So he wanted to add a few lotus flowers to enable Betsy to complete the task. The consistently frugal farmer John wants to add a minimum number of lotus flowers. Of course, the Lotus cannot be placed on the stone. Please help farmer John determine the minimum number of lotus that must be added. The minimum number of steps that Bessie needs to jump from the starting point to the target point is calculated based on the minimum amount of Lotus added. Finally, we also calculate the number of hops with the least number of hops to jump when the minimum quantity of added Lotus is satisfied.
Input
Line 1th: Two integers M, N
2..M + 1 lines: line i + 1, the line i + 1 has N integers, indicating the state of the position: 0 is water; 1 for Lotus; 2 for the Rock; 3 The position for Bessie to begin; 4 for Bessie to go to the target position.
Output
Line 1th: An integer: The minimum number of lotus that needs to be added. If at any rate Bessie could not jump, output-1.
Line 2nd: An integer: The minimum number of steps that Bessie will need to jump from the starting point to the target point, based on the minimum amount of Lotus added. If line 1th outputs-1, this line is not output. Line 3rd: An integer: The number of hops that are added when the minimum number of Lotus, the jump number of steps to the 2nd row of the output value of the jump path if the 1th line output-1, this line does not output.
Sample Input4 8
0 0 0 1 0 0 0 0
0 0 0 0 0 2 0 1
0 0 0 0 0 4 0 0
3 0 0 0 0 0 1 0
Sample Output2
6
2
Output description
Add at least 2 lotus flowers and place them in the ' X ' position.
0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0
0 x 0 0 0 2 0 1 0 0 0 0 0 2 0 1
0 0 0 0 x 4 0 0 0 0 x 0 x 4 0 0
3 0 0 0 0 0 1 0 3 0 0 0 0 0 1 0
Betsy has to take at least 6 steps, with the following two options
0 0 0 C 0 0 0 0 0 0 0 C 0 0 0 0
0 B 0 0 0 2 0 F 0 0 0 0 0 2 0 F
0 0 0 0 d G 0 0 0 0 B 0 D G 0 0
A 0 0 0 0 0 E 0 A 0 0 0 0 0 E 0
HINT Source
Silver
Solution: A very cute deep search, in fact, the truth is also very simple, is in the search for the best program when passing the total number of steps to record, and the number of programs can ...
Unexpectedly took the Rank5, estimated soon to be abused by the TT
1/**************************************************************2Problem:16323 User:hansbug4 language:pascal5 result:accepted6Time:0Ms7Memory:376KB8****************************************************************/9 Ten Const OneXt:Array[1..8] ofLongint= (1,1,-1,-1,2,2,-2,-2); AYtArray[1..8] ofLongint= (2,-2,2,-2,1,-1,1,-1); - var - I,j,k,l,m,n,ans,f,r,ex,ey:longint; theX, Y:Array[0..10010] ofLongint; -A,b,c,d:Array[0.. -,0.. -] ofLongint; -E:Array[0.. +,0.. +] ofInt64; - procedureBfs;inline; + varNx,ny,ty,tx,i,j,k,l,ta:longint; - begin + A whileF<>r Do at begin -nx:=x[f];ny:=y[f];inc (f); - fork:=1 to 8 Do - begin -tx:=nx+xt[k];ty:=ny+Yt[k]; - if(tx<1)or(ty<1)or(tx>n)or(ty>m)or(a[tx,ty]=2) Thencontinue; inTa:=b[nx,ny]+longint (a[tx,ty]=0); - ifTa<b[tx,ty] Then to begin +b[tx,ty]:=ta; -c[tx,ty]:=c[nx,ny]+1; thee[tx,ty]:=E[nx,ny]; * ifD[tx,ty]<>0 Thencontinue; $d[tx,ty]:=1; x[r]:=TX;Panax Notoginsengy[r]:=Ty;inc (r); - End the Else + begin A ifTa=b[tx,ty] Then the begin + if(c[nx,ny]+1) <c[tx,ty] Then - begin $c[tx,ty]:=c[nx,ny]+1; $e[tx,ty]:=E[nx,ny]; - ifD[tx,ty]<>0 Thencontinue; -d[tx,ty]:=1; thex[r]:=tx;y[r]:=Ty; - Inc (R);Wuyi End the Else - begin Wu if(c[nx,ny]+1) =c[tx,ty] Then - begin About Inc (E[tx,ty],e[nx,ny]); $ ifD[tx,ty]<>0 Thencontinue; -d[tx,ty]:=1; -x[r]:=tx;y[r]:=Ty;inc (r); - End; A End; + End; the End; - End; $d[nx,ny]:=0; the End; the End; the begin thef:=0; r:=1; - readln (n,m); in fori:=1 toN Do the forj:=1 toM Do the begin About read (a[i,j]); the ifJ=m ThenReadln; theb[i,j]:=maxlongint;c[i,j]:=Maxlongint; the ifa[i,j]=3 Then + begin -d[i,j]:=1; thex[0]:=i;y[0]:=j;b[i,j]:=0;Bayic[i,j]:=0; thee[i,j]:=1; the End - Else - ifa[i,j]=4 Then the begin theex:=i;ey:=J; the End; the End; - BFS; the ifB[ex,ey]=maxlongint Then the begin theWriteln (-1);94 Halt; the End the Else the begin98 Writeln (B[ex,ey]); About Writeln (C[ex,ey]); - Writeln (E[ex,ey]);101 End;102 End.
1632: [Usaco2007 feb]lilypad Pond