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? ›

"The eight queens

queens
The queen (♕, ♛) is the most powerful piece in the game of chess. It can move any number of squares vertically, horizontally or diagonally , combining the powers of the rook and bishop. Each player starts the game with one queen, placed in the middle of the first rank next to the king.
https://en.wikipedia.org › wiki › Queen_(chess)
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. The problem was first posed in the mid-19th century.

How to solve 8 queen problem using backtracking? ›

Backtracking Approach
  1. Start in the leftmost column.
  2. If all queens are placed, then return true.
  3. Try all rows in the current column. ...
  4. If placing the queen in the current row and proceeding to solve the rest of the problem leads to a solution, then return true.
Sep 30, 2023

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.

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.

What is the n-queens problem? ›

In chess, a queen can attack horizontally, vertically, and diagonally. The N-queens problem asks: How can N queens be placed on an NxN chessboard so that no two of them attack each other? Below, you can see one possible solution to the N-queens problem for N = 4. No two queens are on the same row, column, or diagonal.

How many possible solutions exist for an 8 queen problem? ›

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.

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).

What is the biggest mistake in chess? ›

Mistake #1: Playing too many moves with pawns.

In the opening you should play few moves with the pawns, and they should be aimed to controlling and occupying the center of the board (just like in soccer, it is important to control the midfield, in chess the center of the board is fundamental).

What is the most famous chess problem? ›

"Excelsior" is one of Sam Loyd's most famous chess problems, originally published in London Era in 1861. In 1867, it participated together with five other problems as a set in an international problem tournament. The motto for the full set was "Excelsior" (eng.

What is the 50 rule in chess? ›

The fifty-move rule in chess states that a player can claim a draw if no capture has been made and no pawn has been moved in the last fifty moves (for this purpose a "move" consists of a player completing a turn followed by the opponent completing a turn).

What is the 8 queen problem an example of? ›

The N-Queens problem is a classic puzzle. Its logic involves placing N chess queens on an N×N chessboard, so that no two queens threaten each other. It's a constraint satisfaction problem commonly solved using backtracking algorithms.

What is the 4 queen problem? ›

The 4-queens problem consists of a 4x4 chessboard with 4 queens. The goal is to place the 4 queens on the chessboard such that no two queens can attack each other. Each queen attacks anything in the same row, in the same column, or in the same diagonal. Formulate the state of the 4-queens problem below.

What is the Million queens problem? ›

On the extremely large chessboard with one million queens, for example, 0.143 would be multiplied by one million, coming out to about 143,000. That figure would then be raised to the power of one million, meaning it's multiplied by itself that many times. The final answer is a figure with five million digits.

How many solutions are there for 8 queens on 8 * 8 board 91 93 92 90? ›

Explanation: For 8*8 chess board with 8 queens there are total of 92 solutions for the puzzle.

Which type of algorithm is used to solve 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.

Top Articles
Latest Posts
Article information

Author: Trent Wehner

Last Updated:

Views: 6053

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Trent Wehner

Birthday: 1993-03-14

Address: 872 Kevin Squares, New Codyville, AK 01785-0416

Phone: +18698800304764

Job: Senior Farming Developer

Hobby: Paintball, Calligraphy, Hunting, Flying disc, Lapidary, Rafting, Inline skating

Introduction: My name is Trent Wehner, I am a talented, brainy, zealous, light, funny, gleaming, attractive person who loves writing and wants to share my knowledge and understanding with you.