Eight queens puzzle and BinomialDistribution/Revisited: Difference between pages

From Wikipedia, the free encyclopedia
(Difference between pages)
Content deleted Content added
m Automated conversion
 
m Automated conversion
 
Line 1: Line 1:
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.


:''See also :'' [[BinomialDistribution]]
<h3>The eight queens puzzle as an example problem for algorithm design</h3>

The eight queens puzzle is a good example of a simple but non-trivial problem that can be solved by a [[recursion|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 64<sup>8</sup> 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]].


<h3>Example program in Python</h3>

''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:
* [[chess problem]]
* [[functional programming]]
* [[mathematical game]]

External links to solutions using:
* [http://www.atarimagazines.com/v3n12/Queens8.html Atari BASIC]
* [http://www.liacs.nl/~gusz/Flying_Circus/2.Slides/2.Examples/1.8Queens/ Genetic algorithms]
* [http://www.scdi.org/%7eavernet/projects/jaskell/queens/ Haskell/Java hybrid]
* [http://www.math.utah.edu/%7Ealfeld/queens/queens.html Java]
* [http://www.dcs.ed.ac.uk/home/mlj/demos/queens/ Standard ML]


Other external links:
* http://bridges.canterbury.ac.nz/features/eight.html

[[talk:Eight_queens_puzzle|/Talk]]

Revision as of 00:51, 27 January 2002