projog

5.4. Eight Queens

The goal of the eight queens puzzle is to place eight chess queens on an 8x8 chessboard so that no two queens share the same row, column or diagonal.

Example

queens(Ys) :-
  Ys = [_,_,_,_,_,_,_,_],
  Ys ins 1..8,
  all_different(Ys),
  safe(Ys),
  label(Ys).

safe([]).
safe([Y|Ys]) :-
  no_attack(Y,Ys,1),
  safe(Ys).

no_attack(_,[],_).
no_attack(Y1,[Y2|Ys],D) :-
  D #\= Y1-Y2,
  D #\= Y2-Y1,
  D1 is D+1,
  no_attack(Y1,Ys,D1).

?- queens(X).
X = [1,5,8,6,3,7,2,4]

yes;
X = [1,6,8,3,7,4,2,5]

yes;
X = [1,7,4,6,8,2,5,3]

yes;
X = [1,7,5,8,2,4,6,3]

yes;
X = [2,4,6,8,3,1,7,5]

yes;
X = [2,5,7,4,1,8,6,3]

yes;
X = [2,5,7,1,3,8,6,4]

yes;
X = [2,6,1,7,4,8,3,5]

yes;
X = [2,6,8,3,1,4,7,5]

yes;
X = [2,7,3,6,8,5,1,4]

yes;
X = [2,7,5,8,1,4,6,3]

yes;
X = [2,8,6,1,3,5,7,4]

yes;
X = [3,5,2,8,6,4,7,1]

yes;
X = [3,5,2,8,1,7,4,6]

yes;
X = [3,6,2,5,8,1,7,4]

yes;
X = [3,6,2,7,5,1,8,4]

yes;
X = [3,6,2,7,1,4,8,5]

yes;
X = [3,7,2,8,5,1,4,6]

yes;
X = [3,7,2,8,6,4,1,5]

yes;
X = [3,6,4,2,8,5,7,1]

yes;
X = [3,6,4,1,8,5,7,2]

yes;
X = [3,8,4,7,1,6,2,5]

yes;
X = [3,1,7,5,8,2,4,6]

yes;
X = [3,5,7,1,4,2,8,6]

yes;
X = [3,6,8,1,4,7,5,2]

yes;
X = [3,6,8,1,5,7,2,4]

yes;
X = [3,6,8,2,4,1,7,5]

yes;
X = [3,5,8,4,1,7,2,6]

yes;
X = [4,7,5,2,6,1,3,8]

yes;
X = [4,6,8,2,7,1,3,5]

yes;
X = [4,7,5,3,1,6,8,2]

yes;
X = [4,8,5,3,1,7,2,6]

yes;
X = [4,6,8,3,1,7,5,2]

yes;
X = [4,2,7,3,6,8,1,5]

yes;
X = [4,2,7,3,6,8,5,1]

yes;
X = [4,8,1,3,6,2,7,5]

yes;
X = [4,2,7,5,1,8,6,3]

yes;
X = [4,6,1,5,2,8,3,7]

yes;
X = [4,2,8,5,7,1,3,6]

yes;
X = [4,8,1,5,7,2,6,3]

yes;
X = [4,2,8,6,1,3,5,7]

yes;
X = [4,7,1,8,5,2,6,3]

yes;
X = [4,7,3,8,2,5,1,6]

yes;
X = [4,1,5,8,2,7,3,6]

yes;
X = [4,2,5,8,6,1,3,7]

yes;
X = [4,1,5,8,6,3,7,2]

yes;
X = [5,8,4,1,3,6,2,7]

yes;
X = [5,7,4,1,3,8,6,2]

yes;
X = [5,8,4,1,7,2,6,3]

yes;
X = [5,2,6,1,7,4,8,3]

yes;
X = [5,2,8,1,4,7,3,6]

yes;
X = [5,7,1,3,8,6,4,2]

yes;
X = [5,1,8,4,2,7,3,6]

yes;
X = [5,7,1,4,2,8,6,3]

yes;
X = [5,3,8,4,7,1,6,2]

yes;
X = [5,7,2,4,8,1,3,6]

yes;
X = [5,1,8,6,3,7,2,4]

yes;
X = [5,7,2,6,3,1,4,8]

yes;
X = [5,7,2,6,3,1,8,4]

yes;
X = [5,3,1,6,8,2,4,7]

yes;
X = [5,1,4,6,8,2,7,3]

yes;
X = [5,2,4,6,8,3,1,7]

yes;
X = [5,3,1,7,2,8,6,4]

yes;
X = [5,2,4,7,3,8,6,1]

yes;
X = [6,3,1,8,4,2,7,5]

yes;
X = [6,3,1,7,5,8,2,4]

yes;
X = [6,3,1,8,5,2,4,7]

yes;
X = [6,4,1,5,8,2,7,3]

yes;
X = [6,8,2,4,1,7,5,3]

yes;
X = [6,4,2,8,5,7,1,3]

yes;
X = [6,1,5,2,8,3,7,4]

yes;
X = [6,3,5,8,1,4,2,7]

yes;
X = [6,3,5,7,1,4,2,8]

yes;
X = [6,2,7,1,4,8,5,3]

yes;
X = [6,2,7,1,3,5,8,4]

yes;
X = [6,3,7,2,8,5,1,4]

yes;
X = [6,3,7,2,4,8,1,5]

yes;
X = [6,3,7,4,1,8,2,5]

yes;
X = [6,4,7,1,8,2,5,3]

yes;
X = [6,4,7,1,3,5,2,8]

yes;
X = [7,1,3,8,6,4,2,5]

yes;
X = [7,2,4,1,8,5,3,6]

yes;
X = [7,2,6,3,1,4,8,5]

yes;
X = [7,3,1,6,8,5,2,4]

yes;
X = [7,3,8,2,5,1,6,4]

yes;
X = [7,4,2,8,6,1,3,5]

yes;
X = [7,4,2,5,8,1,3,6]

yes;
X = [7,5,3,1,6,8,2,4]

yes;
X = [8,2,4,1,7,5,3,6]

yes;
X = [8,2,5,3,1,7,4,6]

yes;
X = [8,3,1,6,2,5,7,4]

yes;
X = [8,4,1,3,6,2,7,5]

yes;

no