| |||||||||
Example solution
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.
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.
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.)
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.
This program written in the Python programming language uses breadth-first search combined with the hard-coded insights that: