"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Solving the Eight Queens Problem Using Backtracking

Solving the Eight Queens Problem Using Backtracking

Published on 2024-11-02
Browse:583

The Eight Queens problem is to find a solution to place a queen in each row on a chessboard such that no two queens can attack each other. The problem can be solved using recursion. In this section, we will introduce a common algorithm design technique called backtracking for solving this problem. The backtracking approach searches for a candidate solution incrementally, abandoning that option as soon as it determines that the
candidate cannot possibly be a valid solution, and then looks for a new candidate.

You can use a two-dimensional array to represent a chessboard. However, since each row can have only one queen, it is sufficient to use a one-dimensional array to denote the position of the queen in the row. Thus, you can define the queens array as:

int[] queens = new int[8];

Assign j to queens[i] to denote that a queen is placed in row i and column j. Figure below (a) shows the contents of the queens array for the chessboard in Figure below (b).

Image description

The search starts from the first row with k = 0, where k is the index of the current row being considered. The algorithm checks whether a queen can be possibly placed in the j_th column in the row for _j = 0, 1, ... , 7, in this order. The search is implemented as follows:

  • If successful, it continues to search for a placement for a queen in the next row. If the current row is the last row, a solution is found.
  • If not successful, it backtracks to the previous row and continues to search for a new placement in the next column in the previous row.
  • If the algorithm backtracks to the first row and cannot find a new placement for a queen in this row, no solution can be found.

The code below gives the program that displays a solution for the Eight Queens problem.

package application;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;

public class EightQueens extends Application {
    public static final int SIZE = 8; // The size of the chess board
    // queens are placed at (i, queens[i])
    // -1 indicates that no queen is currently placed in the ith row
    // Initially, place a queen at (0, 0) in the 0th row
    private int[] queens = {-1, -1, -1, -1, -1, -1, -1, -1};

    @Override // Override the start method in the Application class
    public void start(Stage primaryStage) {
        search(); // Search for a solution

        // Display chess board
        GridPane chessBoard = new GridPane();
        chessBoard.setAlignment(Pos.CENTER);
        Label[][] labels = new Label[SIZE][SIZE];
        for(int i = 0; i = 0 && k 



The program invokes search() (line 20) to search for a solution. Initially, no queens are placed in any rows (line 16). The search now starts from the first row with k = 0 (line 53) and finds a place for the queen (line 56). If successful, place it in the row (line 61) and consider the next row (line 62). If not successful, backtrack to the previous row (lines 58–59).

The findPosition(k) method searches for a possible position to place a queen in row k starting from queen[k] 1 (line 73). It checks whether a queen can be placed at start, start 1, . . . , and 7, in this order (lines 75–78). If possible, return the column index (line 77); otherwise, return -1 (line 80).

The isValid(row, column) method is called to check whether placing a queen at the specified position causes a conflict with the queens placed earlier (line 76). It ensures that no queen is placed in the same column (line 86), in the upper-left diagonal (line 87), or in the upper-right diagonal (line 88), as shown in Figure below.

Image description

Release Statement This article is reproduced at: https://dev.to/paulike/solving-the-eight-queens-problem-using-backtracking-25cc?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3