主题:[原创][下载]N皇后问题所有皇后数的所有解
PASCAL (PREE PASCAL or TRUBO PASCAL)
var
a:array [1..9] of 1..9;
n,t:word;
procedure print;
var
j:byte;
begin
t:=t+1;
write('No. ',t,' ');
for j:=1 to n do
write('(',j,',',a[j],') ');
writeln;
if t mod 24=0 then readln;
end;
function check(s,i:byte):boolean;
var
j:byte;
begin
check:=false;
for j:=1 to s-1 do
if (i=a[j]) or (abs(s-j)=abs(i-a[j])) then exit;
check:=true;
end;
procedure sub(s:byte);
var
i:byte;
begin
if s>n then
print
else
for i:=1 to n do
if check(s,i) then
begin
a[s]:=i;
sub(s+1);
end;
end;
begin
repeat
write('Queen Number: ');
readln(n);
until n in [4..9];
t:=0;
sub(1);
readln;
end.
var
a:array [1..9] of 1..9;
n,t:word;
procedure print;
var
j:byte;
begin
t:=t+1;
write('No. ',t,' ');
for j:=1 to n do
write('(',j,',',a[j],') ');
writeln;
if t mod 24=0 then readln;
end;
function check(s,i:byte):boolean;
var
j:byte;
begin
check:=false;
for j:=1 to s-1 do
if (i=a[j]) or (abs(s-j)=abs(i-a[j])) then exit;
check:=true;
end;
procedure sub(s:byte);
var
i:byte;
begin
if s>n then
else
for i:=1 to n do
if check(s,i) then
begin
a[s]:=i;
sub(s+1);
end;
end;
begin
repeat
write('Queen Number: ');
readln(n);
until n in [4..9];
t:=0;
sub(1);
readln;
end.