ud_labeling([]).
ud_labeling([X|L]):-
	% Choice of the enumerated variable (first-fail strategy)
	% (left-most variable with the smallest domain)
	deleteff(V, [X|L], R),
	% Enumeration of variable V (minimum first)
	indomain(V),
	ud_labeling(R).

no_attack(_, [], _).

no_attack(X, [Y|R], N):-
	X ## Y,
	X ## Y + N,
	Y ## X + N,

	N1 is N + 1,
	no_attack(X, R, N1).

safe([_]).
safe([X|[Y|L]]):-
	  no_attack(X, [Y|L], 1),
	  safe([Y|L]).

nqueens(N, L):-
	length(L, N),
	L :: 1..N,
	safe(L),
	% User-defined labeling procedure
	ud_labeling(L).


% For instance, 3 goals:
% :- nqueens(4,L).
% :- nqueens(8,L).
% :- nqueens(40,L).