1627: [Usaco2007 Dec] Crossing mud time limit:5 Sec Memory limit:64 MB
submit:504 solved:325
[Submit] [Status] Description
Early in the morning 6:00,farmer John left his house and began his routine work: milking Bessie. The night before, the whole farm had just undergone a downpour, and it was not difficult to see that FJ was now facing a vast expanse of muddy land. FJ's house is in the position of the plane coordinates (0, 0), where Bessie's barn is located at coordinates (x, y) ( -500 <= X <=; -500 <= Y <= 500). Of course, FJ also saw all the N (1 <= n <= 10,000) mud in the ground, the first muddy of the coordinates of (a_i, b_i) ( -500 <= a_i <= 500;-500 <= b_i <= 500). Each mud-puddle occupies only the lattice in which it resides. Farmer John naturally did not want to stain his new boots, but he also wanted to get to Betsy's place as soon as possible. In order to count the nasty mud, he has been delayed for some time. If farmer John can only move parallel to the axis, and turns only at coordinates where x and y are integers, then he departs from the door, at least how many ways to go to the barn where Bessie is located? You can think that from the FJ House to the barn there is always at least one path that does not go through any muddy mud.
Input
* Line 1th: 3 integers separated by a space: X, Y and N
* 2nd. N+1 Line: section i+1 behavior 2 integers separated by spaces: a_i and B_i
Output
* Line 1th: Output 1 integers, that is, FJ the minimum distance required to reach Bessie's barn without stepping into the mud.
Sample Input1 2 7
0 2
-1 3
3 1
1 1
4 2
-1 1
2 2
Input Description:
The coordinates of Bessie's barn are (1, 2). Farmer John can see 7 mud-mud, and their coordinates are divided
Do not be (0, 2), (-1, 3), (3, 1), (1, 1), (4, 2), (1, 1), and (2, 2).
The following is a sketch of the Farm: (* For FJ's house, b for Bessie's barn)
4 .....
3. M......
Y 2. . M B m. M.
1. M. M. M. .
0. . * . . . . .
-1 ...
-2-1 0 1 2 3 4 5
X
Sample Output11
HINT
The best routes for John are: (0,0), (11, 0), (12, 0), (12, 1), (12, 2), (12, 3), (12, 4), (+ +, (+), 11), (0,4), (0,3), (1,3), (for all).
Source
Silver
Problem: Fried chicken is a lovely bfs, there is nothing, mainly is to have been to the point of all pruning off, note if set boundaries, it is best to ( -501..501) x ( -501..501) set as the legal boundary-the topic can not be stipulated that this map is limited, but since the mire range for (- 500..500) x ( -500..500), then the extension of the one is (so personally feel that the question is guaranteed to have a workable path this is completely redundant-how can there be no access to the infinite map? And then nothing more ... (Only the BFS can be pruned to this extent 100ms I'm drunk too * ^_^ *)
1 var2 I,j,k,l,m,n,f,r,x,y:longint;3A:Array[-510..510,-510..510] ofLongint;4B:Array[0..1500000,1..2] ofLongint;5 begin6 readln (x,y,n);7Fillchar (A,sizeof (a),0);8 fori:=1 toN Do9 beginTen readln (j,k); Onea[j,k]:=-1; A End; - fori:=-502 to 502 Do - begin thea[i,-502]:=-1; -A[i,502]:=-1; -a[-502, i]:=-1; -a[502, i]:=-1; + End; -f:=1; r:=2; b[1,1]:=0; b[1,2]:=0; +a[0,0]:=1; A whileF<r Do at begin - ifA[b[f,1]+1, B[f,2]]=0 Then - begin -B[r,1]:=b[f,1]+1; -B[r,2]:=b[f,2]; -A[b[r,1],b[r,2]]:=a[b[f,1],b[f,2]]+1; in if(B[r,1]=X) and(B[r,2]=y) Then - begin toWriteln (A[b[r,1],b[r,2]]-1); + Halt; - End; the Inc (R); * End; $ ifA[b[f,1]-1, B[f,2]]=0 ThenPanax Notoginseng begin -B[r,1]:=b[f,1]-1; theB[r,2]:=b[f,2]; +A[b[r,1],b[r,2]]:=a[b[f,1],b[f,2]]+1; A if(B[r,1]=X) and(B[r,2]=y) Then the begin +Writeln (A[b[r,1],b[r,2]]-1); - Halt; $ End; $ Inc (R); - End; - ifA[b[f,1],b[f,2]+1]=0 Then the begin -B[r,1]:=b[f,1];WuyiB[r,2]:=b[f,2]+1; theA[b[r,1],b[r,2]]:=a[b[f,1],b[f,2]]+1; - if(B[r,1]=X) and(B[r,2]=y) Then Wu begin -Writeln (A[b[r,1],b[r,2]]-1); About Halt; $ End; - Inc (R); - End; - ifA[b[f,1],b[f,2]-1]=0 Then A begin +B[r,1]:=b[f,1]; theB[r,2]:=b[f,2]-1; -A[b[r,1],b[r,2]]:=a[b[f,1],b[f,2]]+1; $ if(B[r,1]=X) and(B[r,2]=y) Then the begin theWriteln (A[b[r,1],b[r,2]]-1); the Halt; the End; - Inc (R); in End; the Inc (f); the End; About End. the
1627: [Usaco2007 Dec] crossing the mud land