

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


var count = 0
var c = []
var f = []
 
var nQueens // recursive
nQueens = Fn.new { |row, n|
    for (x in 1..n) {
        var outer = false
        var y = 1
        while (y < row) {
            if ((c[y] == x) || (row - y == (x -c[y]).abs)) {
                outer = true
                break
            }
            y = y + 1
        }
        if (!outer) {
            c[row] = x
            if (row < n) {
                nQueens.call(row + 1, n)
            } else {
                count = count + 1
                if (count == 1) f = c.skip(1).map { |i| i - 1 }.toList
            }
        }
    }
}
 
for (n in 1..14) {
    count = 0
    c = List.filled(n+1, 0)
    f = []
    nQueens.call(1, n)
    System.print("For a %(n) x %(n) board:")
    System.print("  Solutions = %(count)")
    if (count > 0) System.print("  First is %(f)")
    System.print()
}