主题:其中,□代表棋子,○代表空格
zhangyuan
[专家分:630] 发布于 2006-12-21 19:39:00
假定有32个棋子,象下图那样摆放在33格的棋盘上,只有中心格空着。现规定当一个棋子跳过邻近的一个棋子到空格时,就将邻近的棋子从盘上拿掉。写出一个程序来找出一系列跳步,使除了最后留在中心格的一棋子外其余的棋子均被拿掉。(其中,□代表棋子,○代表空格)
□□□
□□□
□□□□□□□
□□□○□□□
□□□□□□□
□□□
□□□
沙发
zhangyuan [专家分:630] 发布于 2006-12-21 19:42:00
type point=record
x,y:byte;
end;
var i,j,m,k:integer;
s:array[1..7,1..7]of -1..1;
way:array[1..1000,1..2]of point;
procedure print(n:byte);
begin
assign(output,'A3_2.out');
rewrite(output);
for i:=1 to n do
writeln('(',way[i,1].x,',',way[i,1].y,') - (',way[i,2].x,',',way[i,2].y,')');
close(output);halt
end;
procedure find(n:byte);
var a,b:shortint;
begin
if n=31 then
begin
if s[4,4]=1 then
print(n)
else exit
end;
for a:=1 to 7 do
for b:=1 to 7 do
if s[a,b]=1 then
begin
if (b>=3)and(s[a,b-1]=1)and(s[a,b-2]=0) then
begin
s[a,b]:=0;
s[a,b-1]:=0;s[a,b-2]:=1;
way[n+1,2].x:=a;way[n+1,2].y:=b-2;
way[n+1,1].x:=a;way[n+1,1].y:=b;
find(n+1);s[a,b]:=1;
s[a,b-1]:=1;s[a,b-2]:=0
end;
if (a<=5)and(s[a+1,b]=1)and(s[a+2,b]=0) then
begin
s[a,b]:=0;
s[a+1,b]:=0;s[a+2,b]:=1;
way[n+1,2].x:=a+2;way[n+1,2].y:=b;
way[n+1,1].x:=a;way[n+1,1].y:=b;
find(n+1);s[a,b]:=1;
s[a+1,b]:=1;s[a+2,b]:=0
end;
if (b<=5)and(s[a,b+1]=1)and(s[a,b+2]=0) then
begin
s[a,b]:=0;
s[a,b+1]:=0;s[a,b+2]:=1;
way[n+1,2].x:=a;way[n+1,2].y:=b+2;
way[n+1,1].x:=a;way[n+1,1].y:=b;
find(n+1);s[a,b]:=1;
s[a,b+1]:=1;s[a,b+2]:=0
end;
if (a>=3)and(s[a-1,b]=1)and(s[a-2,b]=0) then
begin
s[a,b]:=0;
s[a-1,b]:=0;s[a-2,b]:=1;
way[n+1,2].x:=a-2;way[n+1,2].y:=b;
way[n+1,1].x:=a;way[n+1,1].y:=b;
find(n+1);s[a,b]:=1;
s[a-1,b]:=1;s[a-2,b]:=0
end
end
end;
begin
fillchar(s,sizeof(s),1);
s[4,4]:=0;s[1,7]:=-1;
s[2,7]:=-1;s[1,6]:=-1;
s[2,6]:=-1;s[7,7]:=-1;
s[6,7]:=-1;s[7,6]:=-1;
s[6,6]:=-1;s[1,1]:=-1;
s[2,1]:=-1;s[1,2]:=-1;
s[2,2]:=-1;s[7,1]:=-1;
s[6,1]:=-1;s[7,2]:=-1;
s[6,2]:=-1;find(0)
end.
呼~~ 总算想出来了