pascal八皇后问题

来源:百度知道 编辑:UC知道 时间:2024/05/17 21:58:20
八皇后搜索解法,只要程序,有的快!

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