Download
/* 

  Boolean N-Queens problem in Picat.

  Inspired by the B-Prolog model examples/clpb/bqueens.pl.

  This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com
  See also my Picat page: http://www.hakank.org/picat/

*/

% Licenced under CC-BY-4.0 : http://creativecommons.org/licenses/by/4.0/

import util.
import cp.


main => go.

go =>
  bool_queens(16),
  nl.


go2 =>
  _ = findall(_, bool_queens(8)),
  nl.

go3 =>
  bool_queens(100),
  nl.


bool_queens(N) =>

  Qs = new_array(N,N),
  Qs :: 0..1,

  foreach(I in 1..N)
    sum([$Qs[I,J] : J in 1..N]) #= 1
  end,
  foreach(J in 1..N)
    sum([$Qs[I,J] : I in 1..N]) #= 1
  end,
  foreach(K in 1-N..N-1)
    sum([$Qs[I,J] : I in 1..N, J in 1..N, I-J=K]) #=< 1
  end,
  foreach(K in 2..2*N)
    sum([$Qs[I,J] : I in 1..N, J in 1..N, I+J=K]) #=< 1
  end,

  solve([inout],Qs),

  foreach(I in 1..N)
     println([Qs[I,J].to_string() : J in 1..N].join(''))
  end,
  println(queens=[J : I in 1..N, J in 1..N, Qs[I,J] = 1 ]),
  nl.