1432. Chessboard Challenge (Eight Queens Deformation dfs Backtracking)

AcWing: 1432. Chessboard Challenge

Insert picture description here


Put this condition in the beginning

  • There can be at most one piece on each diagonal

I got it wrong, I thought it was just to limit the main diagonal and the sub-diagonal of the entire matrix. Only one piece can be placed at most

In fact, it is the main diagonal and the sub-diagonal corresponding to each chess piece placed at the current position .


Eight queens deformation, dfs enumeration is enough



Current position (row, col)

All positions corresponding to the subdiagonal line: i + j == row + col

Corresponding positions on the main diagonal: Look at other people’s code as n + (row-j) // I’ve learned a lot



AC Code

import java.util.*;
import static java.lang.System.out;

public class Main{
    
    
    
    static int ans = 0;
    public static void main(String[] args) {
    
    
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[][] map = new int[n][n];
        
        // cols 记录当前列是否被访问过、 dia 记录当前副对角线是否被访问过
        boolean[] cols = new boolean[n], dia = new boolean[2 * n];
        dfs(map, 0, cols, dia, "");
        
        out.println(ans);
    }
    
    public static void dfs(int[][] map, int row, boolean[] cols, boolean[] dia, String s){
    
    
        if(row >= map.length) {
    
    
            //out.println(s);
            ans++;
            if(ans <= 3) out.println(s); 
            return ;
        }
        
        int len = map[0].length;
        for(int j = 0; j < len; j++) {
    
    
            
            // 当前列 没有棋子 && 副对角线 没有棋子 && 当前主对角线没有棋子
            if(!cols[j] && !dia[row + j] && map[row][j] == 0) {
    
    
                // 对角线的
                getset(map, row, j, 1);
                cols[j] = true; dia[row + j] = true;
                dfs(map, row + 1, cols, dia, s + (j + 1) + " ");
                // 回溯
                getset(map, row, j, -1);
                cols[j] = false; dia[row + j] = false;
            }
        }
        
    }
    
    // 对角线
    public static void getset(int[][] map, int row, int col, int val){
    
    
        //主对角线
        int mn = Math.min(row, col), rlen = map.length, clen = map[0].length;
        int r = row - mn, c = col - mn;
        while(r < rlen && c < clen) {
    
    
            map[r++][c++] += val;
        }
        
        // 副对角线
        //for(int i = 0; i < rlen; i++) {
    
    
        //    for(int j = 0; j < clen; j++) {
    
    
        //        if(i + j == row + col) map[i][j] += val;
        //    }
        //}
    }
    
}




Optimize the main diagonal part

simplify


AC Code

import java.util.*;
import static java.lang.System.out;

public class Main{
    
    
    
    static int ans = 0;
    public static void main(String[] args) {
    
    
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[][] map = new int[n][n];
        
        // cols 记录当前列是否被访问过、 dia 记录当前副对角线是否被访问过、master 记录当前主对角线是否被访问过
        boolean[] cols = new boolean[n], dia = new boolean[2 * n], master = new boolean[2 * n];
        dfs(map, 0, cols, dia, master, "");
        
        out.println(ans);
    }
    
    public static void dfs(int[][] map, int row,
    							 boolean[] cols, boolean[] dia, boolean[] master, String s){
    
    
    							 
        if(row >= map.length) {
    
    
            //out.println(s);
            ans++;
            if(ans <= 3) out.println(s); 
            return ;
        }
        
        int len = map[0].length;
        for(int j = 0; j < len; j++) {
    
    
            // 当前列 没有棋子 && 副对角线 没有棋子 && 当前主对角线没有棋子
            if(!cols[j] && !dia[row + j] && !master[map.length + (row - j)]) {
    
    
                // 对角线的
                cols[j] = true; dia[row + j] = true; master[map.length + (row - j)] = true;
                
                dfs(map, row + 1, cols, dia, master, s + (j + 1) + " ");
                // 回溯
                cols[j] = false; dia[row + j] = false; master[map.length + (row - j)] = false;
            }
        }
        
    }
    
    
    /**
     * @Discard 废弃
     */
    // 对角线
    //public static void getset(int[][] map, int row, int col, int val){
    
    
    //    //主对角线
    //    int mn = Math.min(row, col), rlen = map.length, clen = map[0].length;
    //    int r = row - mn, c = col - mn;
    //    while(r < rlen && c < clen) {
    
    
    //        map[r++][c++] += val;
    //    }
    //}
    
}



Guess you like

Origin blog.csdn.net/qq_43765535/article/details/112976377