/**************************************************************** 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 = '), readint(N), N >= 4, read_yn('All solutions (y/n)? ', All), read_yn('Output (y/n)? ', Output), statistics(runtime, _), (all(All) => output(Output) => queen_solve(N)), statistics(runtime, [_,T]), write('CPU time = '), write(T), write(' msec'), nl. read_yn(Message, YN) :- write(Message), readline([Char|_]), ([Char] == "y" -> YN = yes; YN = no). queen_solve(N) :- queen(N, Q), (output(yes) -> write(Q), nl; true), all(no), !. 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]).