/* Fancy queens in Picat. From Adrian Groza "Modelling Puzzles in First Order Logic" """ Puzzle 95. Fancy queens I have placed a queen in one of the white squares of the board shown. Place 7 more queens in white squares so that no 2 of the 8 queens are in line horizontally, vertically, or diagonally (adapted from puzzle 113 from Kordemsky 1992). """ And: A queen cannot be placed on the two main diagonals. x______x _x____x_ __x__x__ ___xx___ ___xx___ __x__x__ qx____x_ x______x This is the unique solution (which row for each column): [7,4,2,8,6,1,3,5] _____Q__ __Q_____ ______Q_ _Q______ _______Q ____Q___ Q_______ ___Q____ Cf nqueens.pi and in general *queen*.pi This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go => N = 8, Q = new_list(N), Q :: 1..N, Q[1] #= 7, % Place a queen at row 7 for column 1 all_different(Q), all_different($[Q[I]-I : I in 1..N]), all_different($[Q[I]+I : I in 1..N]), foreach(I in 1..N) Q[I] #!= I, Q[I] #!= N-I+1 end, solve($[ff],Q), println(Q), foreach(J in 1..N) foreach(I in 1..N) if Q[I] == J then print("Q") else print("_") end end, nl end, fail, nl.