极烦的bfs·迷宫

var i,j,n,m,x,y,tot:integer;
    a:array[1..1000,1..1000] of char;
    que:array[1..1000,1..2] of integer;
    v:array[1..1000,1..1000] of boolean;
    xx:array[1..4] of integer = (0,1,0,-1);
    yy:array[1..4] of integer = (1,0,-1,0);
procedure bfs;
var head,tail,tl:integer;
    f:boolean;
begin
 f:=true;
 que[1,1]:=1; que[1,2]:=1;
 head:=1; tail:=1;tl:=1;
 while (head<=tail)and f do
  begin
  inc(tot);
   for j:=head to tail do
   for i:=1 to 4 do
    begin
     x:=que[j,1]+xx[i];
     y:=que[j,2]+yy[i];
     if (x>0) and (x<=n) and (y>0) and (y<=n) and (v[x,y])and f then
      begin
       if (x=n)and(y=n) then f:=false;
       inc(tl);
       que[tl,1]:=x;
       que[tl,2]:=y;
       v[x,y]:=false;
      end;
    end;
    tail:=tl;
    inc(head);
  end;
  writeln(tot);
end;


  begin
    readln(n);
    fillchar(v,sizeof(v),false);
    x:=1;
    y:=1;
    for i:=1 to n do
     begin
      for j:=1 to n do
       begin
        read(a[i,j]);
        if a[i,j]='.' then v[i,j]:=true;
       end;
       readln;
      end;
    bfs;
  end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值