This article shows algorithmic solutions to the eight queens puzzle implemented in various computer programming languages. For specific outcomes of these algorithms, see the main eight queens puzzle article.
Board representations
* The naive representation is a 2-dimensional bool [N] [N] array indicating for every square whether it contains a queen.:* Partial solutions simply have less than N queens marked.:* Queen conflicts are hard to check: you need to go in all 8 directions looking for occupied squares.
* In a valid solution, there is exactly one queen in each row. So it's sufficient to use an int [N] array telling for each row at which column the queen is. This representation is more compact, efficient and easy to use.:* Queen conflicts are easier to check:::* There can be no horizontal conflicts - there is no way to represent 2 queens in the same row.::* There is a vertical conflict if any two columns in the list are equal. (In other words, a valid solution is a permutation of N numbers.)::* There is a diagonal conflict if the absolute difference between two columns in the list equals the absolute difference between their positions in the list (i.e. their rows).:* Partial solutions with queens only at first M rows (M < N) can be represented by shorter lists.
A standard backtracking solution
Backtracking improves upon brute-force search by building solutions step-by-step, and being able to discard a partial solution together with all solutions that would be built from it. This works for the 8-queens problem: we start from an empty board and add queens one-by-one; the moment we add a queen that creates a conflict, we know adding more queens will not remove the conflict, so we can avoid all solutions that would be generated from it.
A backtrackingdepth-first search (DFS) solution in C:
The Python functions below can generate all solutions for an "n"-queens problem, using a recursivebreadth-first search (BFS). Note that using BFS for the 8-queens problem is silly, since all solutions lie at the same depth (8), so there is no benefit compared to DFS — and BFS requires more memory.
A constraint logic programming solution
The constraint logic programming (over finite domains) approach to this kind of problem is very efficient. The GNU Prolog program below resolved a 100-queens problem in less than a tenth of a second. It finds a permutation of the first "n" naturals such that the distance between any two is not the normal distance (for example, 1 is normally three away from 4).
/* Generates a list which represents a single solution with the specified length and ensures that the list has each value from 1 to N once and only once. */nqueens(N,Ls) :- length(Ls,N), fd_domain(Ls,1,N), fd_all_different(Ls), constraint_queens(Ls), fd_labeling(Ls, [variable_method(random)] ).
/* Ensures that all positions are valid */constraint_queens( [] ).constraint_queens( [X|Xs] ) :- noattack(X,Xs,1), constraint_queens(Xs).
/* Ensures that no queens share diagonals */noattack(_, [] ,_).noattack(X, [Y|Xs] ,N) :- X#=Y+N, X#=Y-N, T=N+1, noattack(X,Xs,T).
An iterative solution
The following J verb generates all solutions for an "n"-queens problem, using an iterative approach.
A "k"-arrangement x has "k" queens on a "k"-by-"n" board where none ofqueens attack each other. To generate all "k+1"-arrangements leading from x,place a queen on all the places on row "k" which are not on the any of the columns or diagonals attacked by the queens in x. The "1"-arrangements are 0, 1, 2, ..., "n"-1; the "n"-arrangements are all the solutions to the "n"-queens problem.
For example, 0 2, 0 3, and 0 4 are valid 2-arrangements for the 8-queens problem. The 3-arrangements leading from these are: 0 2 4 0 2 5 0 2 6 0 2 7 0 3 1 0 3 5 0 3 6 0 3 7 0 4 1 0 4 6 0 4 7
Here is a C# example, built on the .NET Framework 2.0, for an iterative approach.
Puzzle — For other uses, see Puzzle (disambiguation). Puzzle solving redirects here. For the concept in Thomas Kuhn s philosophy of science, see normal science. Part of a series on Puzzles … Wikipedia
Chess puzzle — A chess puzzle is a puzzle in which knowledge of the pieces and rules of chess is used to solve logically a chess related problem. The longstanding popularity of chess has paved the way for a rich tradition of such chess related puzzles and… … Wikipedia
Mathematical chess problem — is a mathematical problem which is formulated using chessboard or chess pieces. These problems belong to recreational mathematics. The most known problems of this kind are Eight queens puzzle or Knight s Tour problems, which have connection to… … Wikipedia
Brute-force search — In computer science, brute force search or exhaustive search, also known as generate and test, is a trivial but very general problem solving technique that consists of systematically enumerating all possible candidates for the solution and… … Wikipedia
Magic square — In recreational mathematics, a magic square of order n is an arrangement of n2 numbers, usually distinct integers, in a square, such that the n numbers in all rows, all columns, and both diagonals sum to the same constant.[1] A normal magic… … Wikipedia
Up to — In mathematics, the phrase up to xxxx indicates that members of an equivalence class are to be regarded as a single entity for some purpose. xxxx describes a property or process which transforms an element into one from the same equivalence class … Wikipedia
Soma cube — The Soma cube is a solid dissection puzzle invented by Piet Hein during a lecture on quantum mechanics conducted by Werner Heisenberg. Seven pieces made out of unit cubes must be assembled into a 3x3x3 cube. The pieces can also be used to make a… … Wikipedia
Min-conflicts algorithm — The min conflicts algorithm is a search algorithm to solve constraint satisfaction problems (CSP problems). It assigns random values to all the variables of a CSP. Then it selects randomly a variable, whose value conflicts with any constraint of… … Wikipedia
List of distributed computing projects — A list of distributed computing projects. Berkeley Open Infrastructure for Network Computing (BOINC) The Berkeley Open Infrastructure for Network Computing (BOINC) platform is currently the most popular volunteer based distributed computing… … Wikipedia