Eight queens puzzle

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Conversion script (talk | contribs) at 00:23, 27 January 2002 (Automated conversion). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

The eight queens puzzle is the problem of putting eight chess queens on an 8x8 chessboard such that none of them is able to attack 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 pieces should share the same row, column, or diagonal.

The eight queens problem has 92 distinct solutions, or 12 distinct solutions if symmetry operations are taken into consideration.

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 that can be solved by 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 algorithm, which considers all 648 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. 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.

It is often used as an example problem for non-traditional appraches, such as constraint programming, logic programming or genetic algorithms.


Example program in Python

This is very quickly hacked together - is it correct? Can anyone do better than this, in an elegant language?

# This uses the insights that
# - no two pieces can share the same row
# - any solution for n queens on a nxm board must contain a solution 
#   for n-1 queens on a (n-1)xm board
# - proceeding in this way will always keep the queens in order, and generate
#   each solution only once
def safe_queen(new_row, new_col, sol):
   # check against each piece on each of the n-1 existing rows
   for row in range(new_row):
       if (sol[row] == new_col or
          (sol[row] + row) == (new_col + new_row) or
          (sol[row] - row) == (new_col - new_row)):
              return 0
   return 1
# This tries to add a queen on all columns of row new_row
def add_queen(new_row, width, previous_solutions):
   solutions = []
   for sol in previous_solutions:
       # try to place a queen on each col on row n-1
       for new_col in range(width):
           # print 'trying', new_col, 'on row', new_row, 'in width', width
           if safe_queen(new_row, new_col, sol):
               # If no interference, add this solution to the list
               solutions = solutions + [ sol + [new_col] ]
   return solutions
# This solves the n-queens problem on a board with n rows and width columns
# - the result is a list of solutions 
# - solutions are 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))
for sol in n_queens(8, 8):
  print sol


See also:

External links to solutions using:


Other external links:

/Talk