
queens2(N, L) :-
    generate(Ln,1,N),            
    permutation(Ln, L),        % enumere N ! cas
    check_constrain_all(L).
generate([N],N,N).
generate([S|Xs],S,N):-
    S < N,
    S1 is S+1,
    generate(Xs,S1,N).


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

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

no_threat(X, Y, I) :-
  Xd1 is X+I,
  Xd1 \= Y,
  Xd2 is X-I,
  Xd2 \= Y.


/*
| ?-  queens2(8,L).

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

(24 ms) yes
| ?- 

*/


