

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


/*
 n-queens.pure
 Tectonics:
     pure -c queens.pure -o queens
  or 
     pure -q -i queens.pure
*/
using system;
 
queens n = search n 1 [] with
   search n i p  = [reverse p] if i>n;
                 = cat [search n (i+1) ((i,j):p) | j = 1..n; safe (i,j) p];
   safe (i,j) p  = ~any (check (i,j)) p;
   check (i1,j1) (i2,j2)
                 = i1==i2 || j1==j2 || i1+j1==i2+j2 || i1-j1==i2-j2;
end;
 
compiling || (puts("queens 4: " + str(queens 4)) $$ 
              puts("Solutions to queens 7: " + str(#queens 7)));