用PASCAL语言解决八皇后问题~急!!
发布网友
发布时间:2小时前
我来回答
共1个回答
热心网友
时间:2小时前
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.
还有很多方法可以用百度查查