The 8-Queens Problem (2024)

The Problem

The 8-queens problem can be defined as follows: Place 8 queens on an (8 by 8) chess board such that none of the queens attacks any of the others. A configuration of 8 queens on the board is shown in figure 1, but this does not represent a solution as the queen in the first column is on the same diagonal as the queen in the last column.

The 8-Queens Problem (1)
Figure 1: Almost a solution of the 8-queens problem

Searching for a Solution

This problem can be solved by searching for a solution. The initial state is given by the empty chess board. Placing a queen on the board represents an action in the search problem. A goal state is a configuration where none of the queens attacks any of the others. Note that every goal state is reached after exactly 8 actions.

This formulation as a search problem can be improved when we realize that, in any solution, there must be exactly one queen in each of the columns. Thus, the possible actions can be restricted to placing a queen in the next column that does not yet contain a queen. This reduces the branching factor from (initially) 64 to 8.

Furthermore, we need only consider those rows in the next column that are not already attacked by a queen that was previously on the board. This is because the placing of further queens on the board can never remove the mutual attack and turn the configuration into a solution.

The EightQueensApp Java Application

EightQueensApp.jar is a Java application that explores the above search space using a number of (uninformed) search strategies. Download the application and double-click it. Alternatively, run the command "java -jar EightQueensApp.jar" from the command line. Either should bring up a window that looks essentially like the one shown in figure 2.

The 8-Queens Problem (2)
Figure 2: The window of the application

To search for a solution, first select a search strategy. Next, there are some configuration options for the search process. If the search space is to be searched as a graph, multiple paths leading to the same node will usually only be explored once. In a tree, search states that can be reached by multiple paths will also be explored multiple times. However, given our formulation of the search problem, there can only be one path to every state. The number of states to be generated can be limited to the given value, resulting in the search being abandoned at that point. For a depth-first search it is also possible to set a depth limit, meaning no states at a greater depth will be explored.

Finally, a trace of the search can be written to the window and/or a text file. The operation performed by a search engine consists of selecting a current search node and generating its successors, and the trace reflects this process. For example, the line:

current state: 11: <State (2qs): 7 3 0 0 0 0 0 0>>

indicates that the selected node contains the given state. In this state 2 queens are on the board. The following numbers then indicate the positions of all queens on the board, starting with the first column. In the example, the first queen is in row 7 (numbering of rows starts from 0 here), and the second queen is in row 3. The remaining zeros can be ignored as there are only two queens on the board.

The lines following this one in the trace describe the successor states that have been generated, for example:

successor state: 59: <State (3qs): 7 3 6 0 0 0 0 0>successor state: 60: <State (3qs): 7 3 1 0 0 0 0 0>successor state: 61: <State (3qs): 7 3 0 0 0 0 0 0>

So, expanding the given search node (11) resulted in three new search nodes (59, 60 and 61).

Even with tracing off the window will display some information about the search (which is not part of the trace). It will show how many states have been explored (the goal test has been performed and successors have been generated) and how many states have been generated (explored states plus fringe nodes). If a solution is found this will also be printed. Finally, the elapsed time taken to perform the search is printed. Note that writing the trace usually takes more time than searching itself.

References

S. Russell and P. Norvig. Artificial Intelligence: A Modern Approach, chapter 3. Prentice Hall, 2nd edition, 2003.

Wikipedia.org: Eight queens puzzle

The 8-Queens Problem (2024)

FAQs

The 8-Queens Problem? ›

Constructing and counting solutions when n = 8

How many solutions are possible for 8 queens? ›

"The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens threaten each other; thus, a solution requires that no two queens share the same row, column, or diagonal. There are 92 solutions.

What is the best algorithm for the 8 queens problem? ›

Which algorithm is used to solve 8 queens problem? One common algorithm used to solve the 8 Queens Problem is the backtracking algorithm, which tries to place queens on the chessboard column by column, checking if each placement is valid and backtracking if it is not.

How many solutions for the N-queens problem? ›

An alternate way of expressing the problem is to place eight “anythings” on an eight by eight grid such that none of them share a common row, column, or diagonal. It has long been known that there are 92 solutions to the problem. Of these 92, there are 12 distinct patterns.

How many fundamental solutions are there for the eight queen puzzle 92 10 11 12? ›

Explanation: For 8*8 chess board with 8 queens there are total of 92 solutions for the puzzle. There are total of 12 fundamental solutions to the eight queen puzzle.

Is the 8 queens problem solvable? ›

The eight queens puzzle has 92 distinct solutions. If solutions that differ only by the symmetry operations of rotation and reflection of the board are counted as one, the puzzle has 12 solutions.

How to solve an 8 queen puzzle? ›

The algorithm starts by placing a queen on the first column, then it proceeds to the next column and places a queen in the first safe row of that column. If the algorithm reaches the 8th column and all queens are placed in a safe position, it prints the board and returns true.

What is the best heuristic for the 8 puzzle? ›

A good heuristic for the 8-puzzle is the number of tiles out of place. A better heuristic is the sum of the distances of each tile from its goal position ("Manhattan distance"). An even better heuristic takes into account the number of direct adjacent tile reversals present.

What is the complexity of the 8 queens problem? ›

Approach: Bruteforce

A simple brute-force solution would be to generate all possible chess boards with 8 queens. Accordingly, there would be N^2 positions to place the first queen, N^2 – 1 position to place the second queen and so on. The total time complexity, in this case, would be O(N^(2N)), which is too high.

How to put 8 queens on a chessboard without threatening each other? ›

Placing queens on a chessboard using the knight's move to separate them can be quite a good strategy for playing eight queens. If you remove the black knights from Figure 1a and replace the four white knights with four queens, then no two queens are threatening each other (Figure 1b).

Is 8 queens problem np complete? ›

The problem of putting eight queens on the chess board so as no queen attacks another is a solved problem, as is placing n queens on an nxn board. However if you place some queens on the board and ask for a completion then the problem is NP complete.

What is the backtracking strategy for the 8 queens problem? ›

Backtracking Approach

Start in the leftmost column. If all queens are placed, then return true. Try all rows in the current column. For each row, if the queen can be placed safely in this row, then mark this cell and try to solve the problem recursively for the rest of the columns.

What is the best way to solve n queens? ›

N Queen Problem using Backtracking:

In the current column, if we find a row for which there is no clash, we mark this row and column as part of the solution. If we do not find such a row due to clashes, then we backtrack and return false. Try all rows in the current column.

Which algorithm is used to solve the 8 queen problem? ›

Backtracking algorithm is used to solve the 8 Queens problem.

What is the 8 Knights problem in chess? ›

The problem aims to place 8 knights on a chess board in such a way that each row/column is occupied by a single knight and no knight removes the other one from the chess board. Employment of different shifting techniques namely: Vertical, Horizontal, Alternate, Diagonal & L shaped have led to solution generation.

How can you tell how many solutions an equation has? ›

If we can solve the equation and get something like x=b where b is a specific number, then we have one solution. If we end up with a statement that's always false, like 3=5, then there's no solution. If we end up with a statement that's always true, like 5=5, then there are infinite solutions.. Created by Sal Khan.

How do you determine how many solutions a system will have? ›

A system of two equations can be classified as follows: If the slopes are the same but the y-intercepts are different, the system has no solution. If the slopes are different, the system has one solution. If the slopes are the same and the y-intercepts are the same, the system has infinitely many solutions.

What is the solution for the 8 queens problem using the back tracking approach? ›

Backtracking Approach

Start in the leftmost column. If all queens are placed, then return true. Try all rows in the current column. For each row, if the queen can be placed safely in this row, then mark this cell and try to solve the problem recursively for the rest of the columns.

Top Articles
Latest Posts
Article information

Author: Neely Ledner

Last Updated:

Views: 5789

Rating: 4.1 / 5 (42 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Neely Ledner

Birthday: 1998-06-09

Address: 443 Barrows Terrace, New Jodyberg, CO 57462-5329

Phone: +2433516856029

Job: Central Legal Facilitator

Hobby: Backpacking, Jogging, Magic, Driving, Macrame, Embroidery, Foraging

Introduction: My name is Neely Ledner, I am a bright, determined, beautiful, adventurous, adventurous, spotless, calm person who loves writing and wants to share my knowledge and understanding with you.