From TheBestLinks.com
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 "independent" 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
- Using pieces other than queens
- For example, on an 8x8 board one can place 32 independent knights, or 14 bishops, or 16 kings. Fairy chess pieces have also been substituted for queens.
- Nonstandard boards
- Polya studied the N-Queens problem on a toroidal ("donut-shaped") board. Other shapes, including three-dimensional "boards", have been studied.
- Domination
- Given an NxN board, find the domination number, which is the minimum number of queens (or other pieces) one needs in order to attack or occupy every square. For the 8x8 board, the queen's domination number is 5.
- Nine queens problem (http://www.chessvariants.org/problems.dir/9queens.html)
- Place nine queens and one pawn on an 8x8 board in such a way that queens don't attack each other. Further generalization of the problem (solution is currently unknown): Given an NxN chess board and M>N queens, find the minimum number of pawns, so that queens and pawns can be set up on the board in such a way, that queens don't attack each other.
- magic square
- In 1992, Demirörs, Rafraf, and Tanik published a method for converting some magic squares into N-queens solutions, and vice versa.
- Latin square
- Chess problem
- Chess-type problem
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 (leaving only 64!/56! = 178,462,987,637,760 possible placements) 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 = 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.
Constraint programming is even more effective on this problem. An 'iterative repair' algorithm typically starts with all queens on the board, for example with one queen per column. It then counts the number of conflicts (attacks), and uses an heuristic to determine how to improve the placement of the queens.
The 'min-conflicts' heuristic - moving the piece with the largest number of conflicts to the square in the same column where the number of conflicts is smallest - is particularly effective: It solves the million-queen problem (a million queens on a 1,000,000 x 1,000,000 chessboard) in less than 50 steps on average.
The 50-step average assumes that the initial configuration is 'reasonably good' - if a million queens all start in the same row, it will obviously take more than 50 steps to fix it. A 'reasonably good' starting point can for instance be found by putting each queen in its column such that it conflicts with the smallest number of queens already on the board.
Note that 'iterative repair', unlike the 'breadth-first' search outlined above, does not guarantee a solution: Like all hillclimbing procedures, it may get stuck on a local optimum (in which case the algorithm may be restarted with a different initial configuration). On the other hand, it can solve problem sizes that are several orders of magnitude beyond the scope of a breadth-first search.
Example program in Python
This program written in the Python programming language uses breadth-first search combined with the hard-coded 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
References
- Watkins, John J. (2004). Across the Board: The Mathematics of Chess Problems. Princeton: Princeton University Press. ISBN 0-691-11503-6.
External links
Links to solutions
- Atari BASIC (http://www.atarimagazines.com/v3n12/Queens8.html)
- Genetic algorithms (http://www.dossier-andreas.net/ai/ga.html)
- Haskell/Java hybrid (http://www.scdi.org/%7eavernet/projects/jaskell/queens/)
- Java (http://www.math.utah.edu/%7Ealfeld/queens/queens.html)
- Standard ML (http://www.dcs.ed.ac.uk/home/mlj/demos/queens/)
- Integer Sequences (http://www.muljadi.org/EightQueens.htm)
- JavaScript (http://www.faust.fr.bw.schule.de/mhb/backtrack/achtdamen/autoacht.htm)
de:Damenproblem
sl:problem osmih dam
Related links
Top visited
0 of
0 links
[no links posted yet]
>> place link >>
Discussion
Last posted
0 of
0 messages
[no messages posted yet]
>> post message >>
Watch
You can
add this article to your own "watchlist" and receive e-mail notification about all changes in this page.