Java recursively solves the eight queens problem

Java recursively solves the eight queens problem

1. Rules

The eight queens problem is a classic backtracking algorithm problem, which aims to find queens 8×8placed on the chessboard 8so that they cannot attack each other ( that is, they cannot be on the same row, column or diagonal ).

2. Code implementation

public class EightQueens {
    
    
    // 棋盘行列数为 8
    private static int SIZE = 8;
    // 用一维数组记录八皇后的摆放方案
    // q[i] 中的 i 表示第几个皇后
    // q[i] 表示该皇后放在哪一列
    private static int[] q = {
    
    -1, -1, -1, -1, -1, -1, -1, -1};

    public static void main(String[] args) {
    
    
        placeQueen(0);
    }

    // 放置皇后,row 表示第几个皇后
    public static void placeQueen(int row) {
    
    
        // 如果 row = 8,说明所有皇后已经摆放完成
        // 直接打印摆放后的棋局
        if (row == SIZE) {
    
    
            printQueen();
        } else {
    
    
            for (int col = 0; col < SIZE; col++) {
    
    
                // 判断皇后是否能放在该格
                // 可以则进行下一个皇后的摆放
                if (isSafe(row, col)) {
    
    
                    q[row] = col;
                    placeQueen(row + 1);
                }
            }
        }
    }

    // 判断该格是否安全
    public static boolean isSafe(int row, int col) {
    
    
        for (int i = 0; i < row; i++) {
    
    
            // 是否与已摆放的皇后位于同一列、位于左上到右下的对角线、位于右上到左下的对角线
            if (q[i] == col || q[i] - i == col - row || q[i] + i == col + row) {
    
    
                return false;
            }
        }
        return true;
    }

    // 打印摆放后的棋局,Q表示皇后
    public static void printQueen() {
    
    
        for (int i = 0; i < SIZE; i++) {
    
    
            for (int j = 0; j < SIZE; j++) {
    
    
                if (q[i] == j) {
    
    
                    System.out.printf("Q ");
                } else {
    
    
                    System.out.printf(". ");
                }
            }
            System.out.println();
        }
        System.out.println();
    }
}
  1. time complexity

    • placeQueen()method is the main method for recursively placing queens. In each recursive level, it needs to traverse each column of the board, so the time complexity is O(N), where Nis the size of the board. Since there are a total of Nqueens to be placed, the total time complexity is O(N^N).
    • isSafe()The method is used to judge whether the current grid is safe, that is, whether it conflicts with an already placed queen. It needs to traverse the placed queens in each recursive level, so the time complexity is O(N). Since there are a total of Nqueens, the total time complexity is O(N^2).
    • printQueen()The method is used to print the chess game after placement, it needs to traverse the entire chessboard, and the time complexity is O(N^2).

    In summary, the total time complexity of the algorithm is O(N^N).

  2. space complexity

    • The algorithm uses a one-dimensional array qto record the placement scheme of the eight queens, and its length is N, so the space complexity is O(N).

    In summary, the total space complexity of the algorithm is O(N).

3. Running results

. . . . . . . Q 
. . . Q . . . . 
Q . . . . . . . 
. . Q . . . . . 
. . . . . Q . . 
. Q . . . . . . 
. . . . . . Q . 
. . . . Q . . . 

Guess you like

Origin blog.csdn.net/qq_45256357/article/details/131981033