Copyright © Philip M. Parker, INSEAD. Terms of Use.

EIGHT QUEENS PUZZLE

Specialty Definition: EIGHT QUEENS PUZZLE

DomainDefinition

Computing

Eight queens puzzle A puzzle in which one has to place eight queens on a chessboard such that no queen is attacking any other, i.e. no two queens occupy the same row, column or diagonal. One may have to produce all possible such configurations or just one. It is a common students assignment to devise a program to solve the eight queens puzzle. The brute force algorithm tries all 64*63*62*61*60*59*58*57 = 178,462,987,637,760 possible layouts of eight pieces on a chessboard to see which ones meet the criterion. More intelligent algorithms use the fact that there are only ten positions for the first queen that are not reflections of each other, and that the first queen leaves at most 42 safe squares, giving only 10*42*41*40*39*38*37*36 = 1,359,707,731,200 layouts to try, and so on. The puzzle may be varied with different number of pieces and different size boards. [Best algorithm?] (1999-07-28). Source: The Free On-line Dictionary of Computing.

Source: compiled by the editor from various references; see credits.

Top     

Specialty Definition: Eight queens puzzle

(From Wikipedia, the free Encyclopedia)

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.

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.

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 search algorithm, which considers all 648 = 248 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. A slightly better brute-force algorithm places a single queen on each row, leading to only 88 = 224 blind placements.

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

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 iff 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 sol[row] + row == new_col + new_row or sol[row] - row == new_col - new_row): return 0 return 1

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

See also

External links

Links to solutions

Source: adapted by the editor from Wikipedia, the free encyclopedia under a copyleft GNU Free Documentation License (GFDL) from the article "Eight queens puzzle."

Top     

Crosswords: EIGHT QUEENS PUZZLE

Specialty definitions using "EIGHT QUEENS PUZZLE": 8 queens problem, 8 queens puzzleeight queens problemLasherismQueens Problem, Queens Puzzle. (references)

Top     

Alternative Orthography: EIGHT QUEENS PUZZLE


Hexadecimal (or equivalents, 770AD-1900s) (references)

45 49 47 48 54      51 55 45 45 4E 53      50 55 5A 5A 4C 45

Leonardo da Vinci (1452-1519; backwards) (references)

        

Binary Code (1918-1938, probably earlier) (references)

01000101 01001001 01000111 01001000 01010100 00100000 01010001 01010101 01000101 01000101 01001110 01010011 00100000 01010000 01010101 01011010 01011010 01001100 01000101

HTML Code (1990) (references)

&#69 &#73 &#71 &#72 &#84 &#32 &#81 &#85 &#69 &#69 &#78 &#83 &#32 &#80 &#85 &#90 &#90 &#76 &#69

ISO 10646 (1991-1993) (references)

0045 0049 0047 0048 0054      0051 0055 0045 0045 004E 0053      0050 0055 005A 005A 004C 0045

Encryption (beginner's substitution cypher): (references)

394341425425155393948532505560604639

Top     



INDEX

1. Crosswords
2. Orthography
3. Bibliography


  

Copyright © Philip M. Parker, INSEAD. Terms of Use.