电子老鼠闯迷宫
Time Limit:1000MS Memory Limit:65536K
Total Submit:156 Accepted:109
Description
如下图12×12方格图,找出一条自入口(2,9)到出口(11,8)的最短路径。
Input
Output
Sample Input
12 //迷宫大小
2 9 11 8 //起点和终点
1 1 1 1 1 1 1 1 1 1 1 1 //邻接矩阵,0表示通,1表示不通
1 0 0 0 0 0 0 1 0 1 1 1
1 0 1 0 1 1 0 0 0 0 0 1
1 0 1 0 1 1 0 1 1 1 0 1
1 0 1 0 0 0 0 0 1 0 0 1
1 0 1 0 1 1 1 1 1 1 1 1
1 0 0 0 1 0 1 0 0 0 0 1
1 0 1 1 1 0 0 0 1 1 1 1
1 0 0 0 0 0 1 0 0 0 0 1
1 1 1 0 1 1 1 1 0 1 0 1
1 1 1 1 1 1 1 0 0 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1
Sample Output
(2,9)->(3,9)->(3,8)->(3,7)->(4,7)->(5,7)->(5,6)->(5,5)->(5,4)->(6,4)->(7,4)->(7,3)->(7,2)->(8,2)->(9,2)->(9,3)->(9,4)->(9,5)->(9,6)->(8,6)->(8,7)->(8,8)->(9,8)->(9,9)->(10,9)->(11,9)->(11,8)
27
var
fx:array[1..4,1..2] of longint;father:array[0..144]of longint;
state:array[0..144,1..2]of longint;
a:array[1..12,1..12]of longint;
i,j,n,k,s,x,y,xx,yy,b,bb:longint;
procedure print(x:longint);
begin
if x=0 then exit;
inc(s);
print(father[x]);
if x<>1 then write('->','(',state[x,1],',',state[x,2],')')
else write('(',state[x,1],',',state[x,2],')');
end;
function check(x1,y1:longint):boolean;
begin
if (x1>=1)and(x1<=n)and(y1>=1)and(y1<=n) then if a[x1,y1]<>1 then exit(true)
else exit(false);
end;
procedure bfs;
var i,x2,y2,head,tail:integer;
begin
head:=0; tail:=1; father[1]:=0;
repeat
inc(head);
state[1,1]:=x; state[1,2]:=y;
for i:=1 to 4 do
if check(state[head,1]+fx[i,1],state[head,2]+fx[i,2]) then
begin
inc(tail);
father[tail]:=head;
state[tail,1]:=state[head,1]+fx[i,1];
state[tail,2]:=state[head,2]+fx[i,2];
a[state[head,1]+fx[i,1],state[head,2]+fx[i,2]]:=1;
if (state[tail,1]=xx)and(state[tail,2]=yy) then begin print(tail); tail:=0;end;
end;
until (head>=tail);
end;
begin
fx[1,1]:=0; fx[1,2]:=1;
fx[2,1]:=0; fx[2,2]:=-1;
fx[3,1]:=1; fx[3,2]:=0;
fx[4,1]:=-1; fx[4,2]:=0;
readln(n);
readln(x,y,xx,yy);
for i:=1 to n do
for j:=1 to n do
read(a[i,j]);
bfs;
writeln;
write(s);
end.