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
總結
以上是生活随笔為你收集整理的Oliver的救援pascal程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求细胞数量pascal题解
- 下一篇: 机器翻译pascal程序