1个回答
展开全部
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;
procedure 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;
procedure 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.
还有很多方法可以用百度查查
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;
procedure 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;
procedure 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.
还有很多方法可以用百度查查
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询