

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


result: new []
 
queens: function [n, i, a, b, c][
    if? i < n [
        loop 1..n 'j [
            if all? @[
                not? contains? a j
                not? contains? b i+j
                not? contains? c i-j
            ] ->
                queens n, i+1, a ++ @[j], b ++ @[i+j], c ++ @[i-j]
 
        ]
    ]
    else [
        if n = size a ->
        'result ++ @[a]
    ]
]
 
BoardSize: 6
 
queens BoardSize, 0, [], [], []
loop result 'solution [
    loop solution 'col [
 
        line: new repeat "-" BoardSize
        line\[col-1]: `Q`
        print line
    ]
    print ""
]