Eight queens puzzle
The eight queens puzzle is the problem of putting eight chess queens on an 8×8 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.
History
Over the years, many mathematicians, including Gauss have worked on this puzzle, which is a special case of the generalized problem of placing n "non-dominating" queens on an n by n chessboard, posed as early as 1850 by Franz Nauck. In 1874, S. Gunther proposed a method of finding solutions by using determinants, and J.W.L. Glaisher refined this approach.
This puzzle was used in the popular early 1990s computer game, The 7th Guest.
Solutions
The eight queens problem has 92 distinct solutions, or 12 distinct solutions if symmetry operations such as rotations and reflections of the board are taken into consideration (via Burnside's lemma.)
Related problems
Setup nine queens and one pawn on 8x8 board in such a way, that queens don't attack each others. Further generalization of the problem (solution is currently unknown): Given NxN chess board and M>N queens. Find the minimum number of pawns, so that queens and pawns can be setup on the board in such a way, that queens don't attack each other.
Paul Muljadi showed that if all 64 squares of the chess board are numbered consecutively from 1 to 64, 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 = 281,474,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 cannot 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 15,720 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
- no two pieces can share the same row;
- any solution for n queens on an n-by-m board must contain a solution for n−1 queens on an (n−1)-by-m board;
- proceeding in this way will always keep the queens in order, and generate each solution only once.
# 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
- Atari BASIC
- Genetic algorithms (doesn't work)
- Haskell/Java hybrid
- Java
- Standard ML
- Integer Sequences