

Solve the eight queens puzzle.



You can extend the problem to solve the puzzle with a board of size   NxN.

For the number of solutions for small values of   N,   see   OEIS: A000170.



Related tasks

 A* search algorithm
 Solve a Hidato puzzle
 Solve a Holy Knight's tour
 Knight's tour
 Peaceful chess queen armies
 Solve a Hopido puzzle
 Solve a Numbrix puzzle
 Solve the no connection puzzle


import sat. 
% import mip.
 
queens_sat(N,Q) =>
  Q = new_array(N,N),
  Q :: 0..1,
 
  foreach (K in 1-N..N-1)
    sum([Q[I,J] : I in 1..N, J in 1..N, I-J==K]) #=< 1
  end,
 
  foreach (K in 2..2*N)
    sum([Q[I,J] :  I in 1..N, J in 1..N, I+J==K]) #=< 1
  end,
 
  foreach (I in 1..N)
    sum([Q[I,J] : J in 1..N]) #= 1
  end,
 
  foreach (J in 1..N)
    sum([Q[I,J] : I in 1..N]) #= 1
  end,
  solve([inout],Q).