algol,n<
begin
comment
Find a solution for the N queen problem.
nqueen18.asc
N Solutions Time
4 2 0.30
5 10 0.75
6 4 2.24
7 40 7.84
8 92 29.82
9 352 121.93
10 724 526.09
11 2680 2482.02
No buffer, N=12:
Time classic: 13968.89
Time turbo: 12368.89 11.5pct
Buffer, N=12:
Time classic: 14069.38
Time turbo: 12469.37 11.4pct
;
integer N, MAXN, nsolutions;
boolean empcol;
boolean empup;
boolean empdo;
boolean one,zero;
integer i;
real clock;
real procedure clock count;
code clock count;
1, 37;
zl , grf p−1 ; RF ≔ clock count; stack[p−1] ≔ RF;
e;
procedure set(x);
value x;
integer x;
begin
integer y;
boolean mask;
y ≔ 0;
mask ≔ empcol ∧ (empdo shift x) ∧ (empup shift (−x));
for y ≔ y while 0≠integer mask do
begin
code mask,y,zero;
3, 46;
3, 44;
2, 46;
arn pa1,nk re1
tk 1,mb a3
gr pa1
srn re1,ck 10
ar pa2,ar c42
gr pa2 V
e1: qq
e;
if x = N then
nsolutions ≔ nsolutions+1
else
begin
empcol ≔ empcol ∧ (zero shift (−y));
empup ≔ empup ∧ (zero shift (x−y));
empdo ≔ empdo ∧ (zero shift (−x−y));
set(x+1);
empcol ≔ empcol ∨ (one shift (−y));
empup ≔ empup ∨ (one shift (x−y));
empdo ≔ empdo ∨ (one shift (−x−y))
end
end
end set;
MAXN ≔ 12; comment maximum size;
one ≔ 1 1 39 0;
zero ≔ 1 0 39 m;
for N ≔ 4 step 1 until MAXN do
begin
nsolutions ≔ 0;
select(17);
writecr;
writetext(«looking onto a »);
writeinteger(«p»,N);
writetext(« × »);
writeinteger(«p»,N);
writetext(« chessboard...»);
writecr;
clock count;
empcol ≔ empup ≔ empdo ≔ false;
for i ≔ 1 step 1 until N do
empcol ≔ empcol ∨ (one shift −i);
for i ≔ −N+1 step 1 until N−1 do
empup ≔ empup ∨ (one shift −i);
for i ≔ 2 step 1 until 2×N do
empdo ≔ empdo ∨ (one shift −i);
set(1);
if nsolutions=0 then
writetext(«NO SOLUTION.»)
else
begin
clock ≔ clock count;
writeinteger(«p»,nsolutions);
writetext(« solutions.»);
writecr;
writetext(«Time: »);
write(«ddddddd.dd»,clock)
end;
writecr
end;
writetext(«done.»);
writecr;
N ≔ N;
end;
t<