pascal最少转弯问题
发布网友
发布时间:2022-10-13 19:32
我来回答
共1个回答
热心网友
时间:2023-11-09 20:02
program p1468;
var
a,c:array[1..100,1..100] of longint;
b:array[1..100,1..20] of longint;
x1,x2,y1,y2,i,j,k,head,tail,m,n,x,y:longint;
Procere sou(ii,jj,kk:longint);
begin
k:=k+kk;
if c[ii,jj]<10000 then exit;
tail:=tail+1;
b[tail,1]:=ii;
b[tail,2]:=jj;
c[ii,jj]:=c[x,y]+1;
end;
Begin
readln(n,m);
for i:=1 to n do
for j:=1 to m do
begin
read(a[i,j]);
c[i,j]:=10000;
end;
readln(x1,y1,x2,y2);
head:=0; tail:=1;
b[1,1]:=x1; b[1,2]:=y1;
c[x1,y1]:=-1;
repeat
head:=head+1;
x:=b[head,1]; y:=b[head,2];
k:=x-1;
while (k>=1) and (a[k,y]=0) do sou(k,y,-1);
k:=x+1;
while (k<=n) and (a[k,y]=0) do sou(k,y,1);
k:=y-1;
while (k>=1)and(a[x,k]=0) do sou(x,k,-1);
k:=y+1;
while (k<=m)and(a[x,k]=0) do sou(x,k,1);
until (head>=tail) or (c[x2,y2]<10000);
writeln(c[x2,y2]);
end.
参考资料:http://zhidao.baidu.com/question/151884199.html