PASCAL递归:迷宫问题
输入n及n*n的迷宫(由0、1组成),输出所有路径方案。以下是我的程序:constx:array[1..4]ofshortint=(1,0,-1,0);y:array[1...
输入n及n*n的迷宫(由0、1组成),输出所有路径方案。
以下是我的程序:
const
x:array[1..4]of shortint=(1,0,-1,0);
y:array[1..4]of shortint=(0,-1,0,1);
var
a:array[1..30,1..30]of shortint;
b,c:array[1..80]of longint;
n,m,ans,s:integer;
procedure init;
var
i,j:integer;
begin
readln(n);
ans:=0;
for i:=1 to n do
begin
for j:=1 to n do
read(a[i,j]);
readln;
end;
end;
procedure print;
var
i:integer;
begin
writeln(ans,':');
for i:=1 to s do
if i=s then write('(',b[i],',',c[i],')')
else write('(',b[i],',',c[i],')->');
writeln;
end;
procedure find_finsh(i,j:longint);
var
k,nx,ny:longint;
begin
b[1]:=1;
c[1]:=1;
for k:=1 to 4 do
begin
nx:=i+x[k];
ny:=j+y[k];
if(nx>n)or(nx<1)or(ny>n)or(ny<1)then continue;
if(a[nx,ny]=0)then
begin
inc(s);
a[i,j]:=1;
b[s]:=nx;
c[s]:=ny;
if(nx=n)and(ny=n)then begin
inc(ans);
print;
s:=1;
end
else find_finsh(nx,ny);
a[i,j]:=0;
end;
end;
end;
begin
{assign(input,'H:\FPC\migong.in');
reset(input);
assign(output,'H:\FPC\migong.out');
rewrite(output);}
init;
s:=1;
find_finsh(1,1);
if ans<>0 then writeln('Total=',ans)
else writeln('Fail!');
{close(input);
close(output);}
end.
输出时输出了,可是是错的。。。
改一下程序 展开
以下是我的程序:
const
x:array[1..4]of shortint=(1,0,-1,0);
y:array[1..4]of shortint=(0,-1,0,1);
var
a:array[1..30,1..30]of shortint;
b,c:array[1..80]of longint;
n,m,ans,s:integer;
procedure init;
var
i,j:integer;
begin
readln(n);
ans:=0;
for i:=1 to n do
begin
for j:=1 to n do
read(a[i,j]);
readln;
end;
end;
procedure print;
var
i:integer;
begin
writeln(ans,':');
for i:=1 to s do
if i=s then write('(',b[i],',',c[i],')')
else write('(',b[i],',',c[i],')->');
writeln;
end;
procedure find_finsh(i,j:longint);
var
k,nx,ny:longint;
begin
b[1]:=1;
c[1]:=1;
for k:=1 to 4 do
begin
nx:=i+x[k];
ny:=j+y[k];
if(nx>n)or(nx<1)or(ny>n)or(ny<1)then continue;
if(a[nx,ny]=0)then
begin
inc(s);
a[i,j]:=1;
b[s]:=nx;
c[s]:=ny;
if(nx=n)and(ny=n)then begin
inc(ans);
print;
s:=1;
end
else find_finsh(nx,ny);
a[i,j]:=0;
end;
end;
end;
begin
{assign(input,'H:\FPC\migong.in');
reset(input);
assign(output,'H:\FPC\migong.out');
rewrite(output);}
init;
s:=1;
find_finsh(1,1);
if ans<>0 then writeln('Total=',ans)
else writeln('Fail!');
{close(input);
close(output);}
end.
输出时输出了,可是是错的。。。
改一下程序 展开
1个回答
展开全部
const
x:array[1..4]of shortint=(1,0,-1,0);
y:array[1..4]of shortint=(0,-1,0,1);
var
a:array[1..30,1..30]of shortint;
b,c:array[1..80]of longint;
n,m,ans,s:integer;
procedure init;
var
i,j:integer;
begin
readln(n);
ans:=0;
for i:=1 to n do
begin
for j:=1 to n do
read(a[i,j]);
readln;
end;
end;
procedure print;
var
i:integer;
begin
writeln(ans,':');
for i:=1 to s do
if i=s then write('(',b[i],',',c[i],')')
else write('(',b[i],',',c[i],')->');
writeln;
end;
procedure find_finsh(i,j:longint);
var
k,nx,ny:longint;
begin
b[1]:=1;
c[1]:=1;
for k:=1 to 4 do
begin
nx:=i+x[k];
ny:=j+y[k];
if(nx>n)or(nx<1)or(ny>n)or(ny<1)then continue;
if(a[nx,ny]=0)then
begin
inc(s);
a[i,j]:=1;
b[s]:=nx;
c[s]:=ny;
if(nx=n)and(ny=n)then begin
inc(ans);
print;
//<--------------------------------
end
else find_finsh(nx,ny);
a[i,j]:=0;
dec(s); //<--------------------------------------------
end;
end;
end;
begin
{assign(input,'H:\FPC\migong.in');
reset(input);
assign(output,'H:\FPC\migong.out');
rewrite(output);}
init;
s:=1;
find_finsh(1,1);
if ans<>0 then writeln('Total=',ans)
else writeln('Fail!');
{close(input);
close(output);}
end.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询