Pascal 骑士游历
源程序&解说&测试数据急求急求!!!!!!说明:在n*n的棋盘上,有一个马在x,y坐标中,求此马遍历(全部走过而不重复)棋盘的方法总数S。输入:n,x,y输出:s...
源程序&解说&测试数据 急求 急求!!!!!!
说明:在n*n的棋盘上,有一个马在x,y坐标中, 求此马遍历(全部走过而不重复)棋盘的方法总数S。
输入:n,x,y
输出:s 展开
说明:在n*n的棋盘上,有一个马在x,y坐标中, 求此马遍历(全部走过而不重复)棋盘的方法总数S。
输入:n,x,y
输出:s 展开
3个回答
展开全部
const maxn=10;
dx:array[1..8] of longint=(1,1,-1,-1,2,2,-2,-2);
dy:array[1..8] of longint=(2,-2,2,-2,1,-1,1,-1);
var a:array[1..maxn,1..maxn] of longint;
n,x,y,total:longint;
procedure print;
var i,j:longint;
begin
for i:=1 to n do
begin
for j:=1 to n do
write(a[i,j]:4);
writeln;
end;
end;
procedure try(dep,x,y:longint);
var i,tx,ty:longint;
begin
a[x,y]:=dep;
if dep=n*n then inc(total)
else for i:=1 to 8 do
begin
tx:=x+dx[i];
ty:=y+dy[i];
if (tx>0)and(tx<=n)and(ty>0)and(ty<=n)and(a[tx,ty]=0)
then try(dep+1,tx,ty)
end;
a[x,y]:=0;
end;
begin
total:=0;
read(n);
read(x,y);
try(1,x,y);
writeln(total);
end.
数据
5 1 1 304
5 1 2 0
5 1 3 56
5 2 3 0
5 3 3 64
4 2 4 0
5 5 5 304
dx:array[1..8] of longint=(1,1,-1,-1,2,2,-2,-2);
dy:array[1..8] of longint=(2,-2,2,-2,1,-1,1,-1);
var a:array[1..maxn,1..maxn] of longint;
n,x,y,total:longint;
procedure print;
var i,j:longint;
begin
for i:=1 to n do
begin
for j:=1 to n do
write(a[i,j]:4);
writeln;
end;
end;
procedure try(dep,x,y:longint);
var i,tx,ty:longint;
begin
a[x,y]:=dep;
if dep=n*n then inc(total)
else for i:=1 to 8 do
begin
tx:=x+dx[i];
ty:=y+dy[i];
if (tx>0)and(tx<=n)and(ty>0)and(ty<=n)and(a[tx,ty]=0)
then try(dep+1,tx,ty)
end;
a[x,y]:=0;
end;
begin
total:=0;
read(n);
read(x,y);
try(1,x,y);
writeln(total);
end.
数据
5 1 1 304
5 1 2 0
5 1 3 56
5 2 3 0
5 3 3 64
4 2 4 0
5 5 5 304
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询