

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


 
templates queens
  def n: $;
  templates addColumn
    def prev: $;
    templates addIfPossible
      def row: $;
      def minor: $ - $prev::length - 1;
      def major: $ + $prev::length + 1;
      // If prev is not an array that contains row, send it on...
      $prev -> \(when <~[<=$row>]> do $ !\)
            -> \(when <?($ -> \[i]($ - $i !\) <~[<=$minor>]>)> do $ !\)
            -> \(when <?($ -> \[i]($ + $i !\) <~[<=$major>]>)> do $ !\)
            -> [ $..., $row] !
    end addIfPossible
    1..$n -> addIfPossible !
  end addColumn
  1..$n -> [$] -> #
  when <[]($n)> do $ !
  otherwise $ -> addColumn -> #
end queens
 
def solutions: [ 8 -> queens ];
'For 8 queens there are $solutions::length; solutions
' -> !OUT::write
 
def columns: ['abcdefgh'...];
'One of them is $solutions(1) -> \[i]('$columns($i);$;' !\);
' -> !OUT::write
 
'For 3 queens there are $:[3 -> queens] -> $::length; solutions
' -> !OUT::write
 