
queens(N, L) :-
     length(L, N),
     fd_domain(L, 1, N),
     constrain_all(L),
     fd_labelingff( L).

constrain_all([]).
constrain_all([X|Xs]) :-
     constrain_between(X, Xs, 1),
     constrain_all(Xs).

constrain_between(_X, [], _N).
constrain_between(X, [Y|Ys], N) :-
     no_threat(X, Y, N),
     N1 is N+1,
     constrain_between(X, Ys, N1).

no_threat(X, Y, I) :-
        X   #\= Y,
        X+I #\= Y,
        X-I #\= Y.
/*
| ?- queens(8, L).  % Enumération par défaut

L = [1,5,8,6,3,7,2,4] ? | 

?- queens(64, L). % Utilisation de l'heuristique firstfail

L = [1,3,5,27,58,4,40,7,39,61,57,30,6,55,42,38,8,46,31,35,32,56,9,
50,33,47,62,34,36,10,21,28,49,53,60,25,14,11,59,43,45,52,48,2,54,
63,12,18,44,17,64,20,37,51,26,41,13,22,16,19,15,23,29,24] ? 

(18 ms) yes
*/
