发布网友 发布时间:2024-11-06 10:02
共1个回答
热心网友 时间:2024-11-06 10:11
program eightqueen;
var
x:array[1..8] of integer;
y:array[1..8,1..8]of boolean;
a,b,c:array[-7..16] of boolean;
i,count:integer;
procere print;{输出棋盘}
var
k,j:integer;
begin
for k:=1 to 8 do begin
for j:=1 to 8 do
y[k,j]:=false; end;
for k:=1 to 8 do
y[k,x[k]]:=true;
for k:=1 to 8 do begin
for j:=1 to 8 do
if y[k,j] then write('Q ')
else write('_ ');
writeln end;
end;
procere try(i:integer);
var
j:integer;
begin
for j:=1 to 8 do
if a[j] and b[i+j] and c[i-j]
then
begin
x[i]:=j;
a[j]:=false;
b[i+j]:=false;
c[i-j]:=false;
if i<8 then try(i+1){试下一个皇后位置}
else begin print; inc(count); writeln; end;
a[j]:=true;{还原}
b[i+j]:=true;
c[i-j]:=true;
end;
end;
begin
for i:=-7 to 16 do
begin
a[i]:=true;
b[i]:=true;
c[i]:=true;
end; count:=0;
try(1); writeln(count);
end.
还有很多方法可以用百度查查