The Eight queens puzzle reference article from the English Wikipedia on 24-Apr-2004
(provided by Fixed Reference: snapshots of Wikipedia from wikipedia.org)

Eight queens puzzle

Sponsorship the way you would do it

Image:chess_l40.png Image:chess_d40.png Image:chess_l40.png Image:chess_d40.png Image:chess_qll40.png Image:chess_d40.png Image:chess_l40.png Image:chess_d40.png
Image:chess_d40.png Image:chess_l40.png Image:chess_d40.png Image:chess_l40.png Image:chess_d40.png Image:chess_l40.png Image:chess_qld40.png Image:chess_l40.png
Image:chess_qll40.png Image:chess_d40.png Image:chess_l40.png Image:chess_d40.png Image:chess_l40.png Image:chess_d40.png Image:chess_l40.png Image:chess_d40.png
Image:chess_d40.png Image:chess_l40.png Image:chess_qld40.png Image:chess_l40.png Image:chess_d40.png Image:chess_l40.png Image:chess_d40.png Image:chess_l40.png
Image:chess_l40.png Image:chess_d40.png Image:chess_l40.png Image:chess_d40.png Image:chess_l40.png Image:chess_d40.png Image:chess_l40.png Image:chess_qld40.png
Image:chess_d40.png Image:chess_l40.png Image:chess_d40.png Image:chess_l40.png Image:chess_d40.png Image:chess_qll40.png Image:chess_d40.png Image:chess_l40.png
Image:chess_l40.png Image:chess_d40.png Image:chess_l40.png Image:chess_qld40.png Image:chess_l40.png Image:chess_d40.png Image:chess_l40.png Image:chess_d40.png
Image:chess_d40.png Image:chess_qll40.png Image:chess_d40.png Image:chess_l40.png Image:chess_d40.png Image:chess_l40.png Image:chess_d40.png Image:chess_l40.png

Example solution

The eight queens puzzle is the problem of putting eight chess queenss on an 8x8 chessboard such that none of them is able to capture any other using the standard chess queen's moves. (Piece colour is ignored, and any piece is assumed to be able to attack any other.) That is to say, no two queens should share the same row, column, or diagonal.

This puzzle was used in the popular early 1990s computer game, The 7th Guest. However, it has a rich history pre-dating this game; the generalized problem of placing n "non-dominating" queens on an n by n chessboard was posed as early as 1850.

Table of contents
1 Solutions
2 Related Problems
3 The eight queens puzzle as an example problem for algorithm design
4 Example program in Python
5 See also
6 External links

Solutions

The eight queens problem has 92 distinct solutions, or 12 distinct solutions if symmetry operationss such as rotations and reflections of the board are taken into consideration (via Burnside's Lemma.)

Related Problems

Paul Muljadi showed that if all sixty-four squares of the chess board are numbered consecutively from one to sixty-four, all 92 distinct solutions can be represented by 92 integer sequences, where each queen placement represents a term.

For example, in the above diagram, the integer sequence is

Since each queen must belong to one and only one row and column (and diagonal), and there are the same number of queens as there are rows and columns, the sum of any such sequence must be fixed. With the integer sequence representation above, the sum must be 8 + sum(1..7) + 8 * sum(1..7) = 260, the magic constant of Eight queens puzzle, for all 92 distinct solutions.

Muljadi also discovered that the magic constant of the Eight queens puzzle is the same Magic constant of Magic Squares of order 8.

The eight queens puzzle as an example problem for algorithm design

The eight queens puzzle is a good example of a simple but non-trivial problem. For this reason, it is often used as an example problem for various programming techniques, including non-traditional approaches such as constraint programming, logic programming or genetic algorithms.

Most often, it is used as an example of a problem which can be solved with a recursive algorithm, by phrasing the n-queen problem inductively in terms of adding a single queen to any solution to the (n-1)-queen problem. The induction bottoms out with the solution to the 0-queen problem, which is an empty chessboard.

This technique is much more efficient than the naive brute-force search algorithm, which considers all 648 = 248 = 2,814,74,976,710,656 possible blind placements of eight queens, and then filters these to remove all placements that place two queens either on the same square or in mutually attacking positions. (Actually, since two queens can not occupy the same space, there are only 64!/56! = 178,462,987,637,760 possible blind placements.) This very poor algorithm will, amongst other things, produce the same results over and over again in all the different permutations of the assignments of the eight queens, as well as repeating the same computations over and over again for the different sub-sets of each solution. A slightly better brute-force algorithm places a single queen on each row, leading to only 88 = 224 = 16,777,216 blind placements.

It is possible to do much better than this. For example, the breadth-first search program below examines only 15720 possible queen placements by constructing the search tree by considering one row of the board at a time, eliminating most possible board positions at a very early stage in their construction.

Example program in Python

See also: Python programming language

This uses the insights that

# Return a list of solutions to the n-queens problem on an
# n-by-width board.  A solved board is expressed as a list of
# column positions for queens, indexed by row.  
# Rows and columns are indexed from zero.
def n_queens(n, width):
    if n == 0:
        return  # one solution, the empty list
    else:
        return add_queen(n-1, width, n_queens(n-1, width))

# Try all ways of adding a queen to a column of row new_row, returning
# a list of solutions.  previous_solutions must be a list of new_row-queens
# solutions.
def add_queen(new_row, width, previous_solutions):
    solutions = []
    for sol in previous_solutions:
        # Try to place a queen on each column on row new_row.
        for new_col in range(width):
            # print 'trying', new_col, 'on row', new_row
            if safe_queen(new_row, new_col, sol):
                # No interference, so add this solution to the list.
                solutions.append(sol + [new_col])
    return solutions

# Is it safe to add a queen to sol at (new_row, new_col)?  Return
# true if so.  sol must be a solution to the new_row-queens problem.
def safe_queen(new_row, new_col, sol):
    # Check against each piece on each of the new_row existing rows.
    for row in range(new_row):
        if (sol[row] == new_col or                  # same column clash
            sol[row] + row == new_col + new_row or  # diagonal clash
            sol[row] - row == new_col - new_row):   # other diagonal
                return 0
    return 1

for sol in n_queens(8, 8):
   print sol

See also

External links

Links to solutions