电子老鼠闯迷宫

电子老鼠闯迷宫
Time Limit:1000MS Memory Limit:65536K
Total Submit:151 Accepted:107
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
 i,m,j,n,px,py,zx,zy,last:longint;
 wayn:array[1..4,1..2]of longint;
 map:array[0..200,0..200]of longint;
 s:array[0..2000,0..4]of longint;
 f:array[0..2000]of longint;
procedure print(x:integer);
begin
 if x=0 then exit;
 inc(m);
 print(f[x]);
 if x<>last then write('(',s[x,1],',',s[x,2],')->')
            else writeln('(',s[x,1],',',s[x,2],')');
end;


function check(y,x:integer):boolean;
begin
 check:=true;
    if (x<1) or (x>n) or (y<1) or (y>n) then check:=false;
    if map[y,x]=1 then check:=false;
  end;


procedure bfs;
var
 tail,head,k,i:longint;
begin
 tail:=1;
 head:=0;
 s[1,1]:=py;
 s[1,2]:=px;
 f[1]:=0;
 repeat
  head:=head+1;
  for i:=1 to 4 do
   if check(s[head,1]+wayn[i,1],s[head,2]+wayn[i,2]) then
    begin
     tail:=tail+1;
     f[tail]:=head;
     s[tail,1]:=s[head,1]+wayn[i,1];
     s[tail,2]:=s[head,2]+wayn[i,2];
     map[s[tail,1],s[tail,2]]:=1;
     if (s[tail,1]=zy)and(s[tail,2]=zx)then
      begin
       m:=0;
       last:=tail;
       print(tail);
       writeln(m);
       tail:=0;
      end;
    end;
 until head>=tail;
end;



begin
 readln(n);
 read(py,px,zy,zx);
 for i:=1 to n do
  for j:=1 to n do
   read(map[i,j]);
 map[py,px]:=1;
 wayn[1,1]:=1;wayn[1,2]:=0;
 wayn[2,1]:=0;wayn[2,2]:=-1;
 wayn[3,1]:=-1;wayn[3,2]:=0;
 wayn[4,1]:=0;wayn[4,2]:=1;
 bfs;
end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值