/****************************************************************
LLP Example
N-Queen Puzzle (posed by Franz Nauch, 1850)
by Naoyuki Tamura (tamura@kobe-u.ac.jp)
Dept. CS, Kobe University
****************************************************************/
main :-
write('N = '),
read(N),
statistics(runtime, _),
queen_solve(N),
statistics(runtime, [_,T]),
write('CPU time = '), write(T), write(' msec'), nl.
queen_solve(N) :-
queen(N, Q),
% write(Q), nl,
fail.
queen_solve(_).
queen(N, Q) :-
n(N) -<> result(Q) -<>
place(N).
place(1) :-
n(N),
(c(1),u(2),d(0)) -<>
solve(N, []).
place(I) :-
I > 1,
I1 is I-1,
U1 is 2*I,
U2 is 2*I-1,
D1 is I-1,
D2 is 1-I,
(c(I),u(U1),u(U2),d(D1),d(D2)) -<>
place(I1).
solve(0, Q) :-
result(Q),
top.
solve(I, Q) :-
I > 0,
c(J),
U is I+J,
u(U),
D is I-J,
d(D),
I1 is I-1,
solve(I1, [J|Q]).