

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


-V BoardSize = 8
 
F underAttack(col, queens)
   I col C queens
      R 1B
   L(x) queens
      I abs(col - x) == queens.len - L.index
         R 1B
   R 0B
 
F solve(n)
   V result = [[Int]()]
   [[Int]] newSolutions
   L(row) 1 .. n
      L(solution) result
         L(i) 1 .. BoardSize
            I !underAttack(i, solution)
               newSolutions.append(solution [+] [i])
      swap(&result, &newSolutions)
      newSolutions.clear()
   R result
 
print(‘Solutions for a chessboard of size ’String(BoardSize)‘x’String(BoardSize))
print()
 
L(answer) solve(BoardSize)
   L(col) answer
      V row = L.index
      I row > 0
         print(‘ ’, end' ‘’)
      print(Char(code' ‘a’.code + row)‘’col, end' ‘’)
   print(end' I L.index % 4 == 3 {"\n"} E ‘      ’)