题解:炒鸡可爱的一道BFS,也没啥,主要就是将已经来过的点全部剪枝掉,注意假如设边界的话,最好将(-501..501)×(-501..501)设为合法的边界——题目中可没规定这个地图是有限的,可是既然泥潭范围为(-500..500)×(-500..500),那就扩展一个就是了(所以个人觉得题目中保证有可行路径这一条完全多余了——对于无限地图怎么可能没有通路?)然后没啥了。。。(才剪枝到这个程度的BFS都能100ms我也是醉了*^_^*)
1 var
2 i,j,k,l,m,n,f,r,x,y:longint;
3 a:array[-510..510,-510..510] of longint;
4 b:array[0..1500000,1..2] of longint;
5 begin
6 readln(x,y,n);
7 fillchar(a,sizeof(a),0);
8 for i:=1 to n do
9 begin
10 readln(j,k);
11 a[j,k]:=-1;
12 end;
13 for i:=-502 to 502 do
14 begin
15 a[i,-502]:=-1;
16 a[i,502]:=-1;
17 a[-502,i]:=-1;
18 a[502,i]:=-1;
19 end;
20 f:=1;r:=2;b[1,1]:=0;b[1,2]:=0;
21 a[0,0]:=1;
22 while f<r do
23 begin
24 if a[b[f,1]+1,b[f,2]]=0 then
25 begin
26 b[r,1]:=b[f,1]+1;
27 b[r,2]:=b[f,2];
28 a[b[r,1],b[r,2]]:=a[b[f,1],b[f,2]]+1;
29 if (b[r,1]=x) and (b[r,2]=y) then
30 begin
31 writeln(a[b[r,1],b[r,2]]-1);
32 halt;
33 end;
34 inc(r);
35 end;
36 if a[b[f,1]-1,b[f,2]]=0 then
37 begin
38 b[r,1]:=b[f,1]-1;
39 b[r,2]:=b[f,2];
40 a[b[r,1],b[r,2]]:=a[b[f,1],b[f,2]]+1;
41 if (b[r,1]=x) and (b[r,2]=y) then
42 begin
43 writeln(a[b[r,1],b[r,2]]-1);
44 halt;
45 end;
46 inc(r);
47 end;
48 if a[b[f,1],b[f,2]+1]=0 then
49 begin
50 b[r,1]:=b[f,1];
51 b[r,2]:=b[f,2]+1;
52 a[b[r,1],b[r,2]]:=a[b[f,1],b[f,2]]+1;
53 if (b[r,1]=x) and (b[r,2]=y) then
54 begin
55 writeln(a[b[r,1],b[r,2]]-1);
56 halt;
57 end;
58 inc(r);
59 end;
60 if a[b[f,1],b[f,2]-1]=0 then
61 begin
62 b[r,1]:=b[f,1];
63 b[r,2]:=b[f,2]-1;
64 a[b[r,1],b[r,2]]:=a[b[f,1],b[f,2]]+1;
65 if (b[r,1]=x) and (b[r,2]=y) then
66 begin
67 writeln(a[b[r,1],b[r,2]]-1);
68 halt;
69 end;
70 inc(r);
71 end;
72 inc(f);
73 end;
74 end.
75