bfs水过就好...
type
rec=record
x,y:longint;
end;
const
walk:array[1..4,1..2] of longint=((1,0),(-1,0),(0,1),(0,-1));
maxn=500; minn=-500;
var
n,stx,sty,x,y :longint;
i :longint;
vis,map :array[-505..505,-505..505] of boolean;
que :array[0..1000010] of rec;
dis :array[-505..505,-505..505] of longint;
procedure bfs;
var
h,tl,curx,cury,cur:longint;
k,tx,ty:longint;
begin
h:=0; tl:=1;
que[1].x:=0; que[1].y:=0;
dis[0,0]:=0; vis[0,0]:=true;
while (h<>tl) do
begin
h:=h mod 1000005+1;
curx:=que[h].x; cury:=que[h].y; cur:=dis[curx,cury];
for k:=1 to 4 do
begin
tx:=curx+walk[k,1]; ty:=cury+walk[k,2];
if (tx>minn) and (tx<=maxn) and (ty>minn) and(ty<=maxn) then
if not map[tx,ty] and not vis[tx,ty] then
begin
vis[tx,ty]:=true;
tl:=tl mod 1000005+1;
que[tl].x:=tx; que[tl].y:=ty; dis[tx,ty]:=cur+1;
if (tx=stx) and (ty=sty) then exit;
end;
end;
end;
end;
begin
read(stx,sty,n);
for i:=1 to n do
begin
read(x,y);
map[x,y]:=true;
end;
bfs;
writeln(dis[stx,sty]);
end.
——by Eirlys