{o是原地图,u是判重,q是BFS队,f是第几步}
program migong;
var i,j,n,m,head,tail:longint;
o:array[0..21,0..21]of boolean;
u:array[0..21,0..21]of boolean;
q:array[1..2,1..400]of longint;
f:array[1..400]of longint;
x,y:integer;
ch:char;
begin
fillchar(o,sizeof(o),false);
fillchar(u,sizeof(u),false);
readln(n,m);
for i:=1 to n do
begin
for j:=1 to m do
begin
read(ch);
if ch='0' then o[i,j]:=true;
end;
readln;
end;
read(q[1,1],q[2,1]);
u[q[1,1],q[2,1]]:=true;
read(x,y);
o[x,y]:=true;
head:=1;
tail:=1;
repeat
i:=q[1,head];
j:=q[2,head];
if o[i+1,j] then begin inc(tail);q[1,tail]:=i+1;q[2,tail]:=j;f[tail]:=f[head]+1;if (q[1,tail]=x)and(q[2,tail]=y) then begin write(f[tail]);halt;end;if u[q[1,tail],q[2,tail]] then dec(tail) else u[q[1,tail],q[2,tail]]:=true;end;
if o[i-1,j] then begin inc(tail);q[1,tail]:=i-1;q[2,tail]:=j;f[tail]:=f[head]+1;if (q[1,tail]=x)and(q[2,tail]=y) then begin write(f[tail]);halt;end;if u[q[1,tail],q[2,tail]] then dec(tail) else u[q[1,tail],q[2,tail]]:=true;end;
if o[i,j-1] then begin inc(tail);q[1,tail]:=i;q[2,tail]:=j-1;f[tail]:=f[head]+1;if (q[1,tail]=x)and(q[2,tail]=y) then begin write(f[tail]);halt;end;if u[q[1,tail],q[2,tail]] then dec(tail) else u[q[1,tail],q[2,tail]]:=true;end;
if o[i,j+1] then begin inc(tail);q[1,tail]:=i;q[2,tail]:=j+1;f[tail]:=f[head]+1;if (q[1,tail]=x)and(q[2,tail]=y) then begin write(f[tail]);halt;end;if u[q[1,tail],q[2,tail]] then dec(tail) else u[q[1,tail],q[2,tail]]:=true;end;
inc(head);
until head>tail;
writeln('No Answer!');
end.
这是我运行你程序的结果:
4 4
0 0 0 0
1 1 1 0
0 0 1 0
0 0 0 0
1 1
3 2
No Answer!