Oliver的救援pascal程序

这题有点像电子老鼠闯迷宫,也是用广搜来做的

我是用字符来输入的


const
dx:array[1..4]of longint=(1,-1,0,0);
dy:array[1..4]of longint=(0,0,1,-1);


var
px,py,x,y,n,s,tail:longint;
a:array[-1..2000,-1..2000]of char;
father:array[1..2000000]of longint;
state:array[1..1000000,1..2]of longint;


procedure init;
var
i,j:longint;
begin
    readln(n);
    for i:=1 to n do
    begin
        for j:=1 to n do
        read(a[i,j]);
        readln;
    end;
    readln(px,py,x,y);
end;


function check(x1,y1:longint):boolean;
begin
    check:=true;
    if a[x1,y1]='1' then check:=false;
    if (x1>n)or(y1>n)or(y1<1)or(x1<1) then check:=false;
end;


procedure print(x:longint);
begin
    if x=0 then exit;
    inc(s);
    print(father[x]);
end;


procedure bfs;
var
head,k:longint;
begin
    head:=0;tail:=1;state[1,1]:=px;state[1,2]:=py;a[px,py]:='1';
    father[1]:=0;
    repeat
         inc(head);
         for k:=1 to 4 do
         if check(state[head,1]+dx[k],state[head,2]+dy[k]) then
         begin
             inc(tail);
             father[tail]:=head;
             state[tail,1]:=state[head,1]+dx[k];
             state[tail,2]:=state[head,2]+dy[k];
             a[state[tail,1],state[tail,2]]:='1';
             if (state[tail,1]=x)and(state[tail,2]=y) then
             begin
                 print(father[tail]);
                 tail:=0;
                 writeln(s);
                 halt;
             end;
         end;
    until head>=tail;
end;
begin
    init;
    bfs;
end.


原文地址:https://www.cnblogs.com/YYC-0304/p/9500230.html