天天看点

LeetCode N皇后 解数独(回溯算法)

文章目录

  • ​​51. N 皇后​​
  • ​​37. 解数独​​

51. N 皇后

LeetCode N皇后 解数独(回溯算法)
LeetCode N皇后 解数独(回溯算法)

我们已知一行只能有一个皇后,所以可以用一维数组chessboard表示皇后的位置,即(i, chessboard[i])表示第i个皇后的位置

通过上图全排列,剪枝的方式可以获得满足规则的皇后位置,验证是否冲突时,我们只需要验证和前面的皇后是否冲突即可,因为我们是一行一行放置皇后

class Solution {
public:
    vector<int> chessboard;
    vector<vector<string>> ans;

    bool isCollide(int pos){
        for(int i = 0; i < pos; i++){
            if(chessboard[i] == chessboard[pos] || abs(i - pos) == abs(chessboard[i] - chessboard[pos])){
                return true;
            }
        }
        return false;
    }

    void func(int startIndex, int n){
        if(startIndex == n){
            vector<string> tmp;
            for(int i = 0; i < n; i++){
                string path(n, '.');
                path[chessboard[i]] = 'Q';
                tmp.push_back(path);
            }
            ans.push_back(tmp);
        }
        for(int i = startIndex; i < n; i++){
            // 这里开始一行一行放皇后,一行放一个
            swap(chessboard[startIndex], chessboard[i]);
            if(!isCollide(startIndex)){
                // 当前摆放的位置(startIndex, chessboard[startIndex])是否和前面的皇后冲突
                func(startIndex + 1, n);
            }
            swap(chessboard[startIndex], chessboard[i]);
        }
    }

    vector<vector<string>> solveNQueens(int n) {
        for(int i = 0; i < n; i++){
            chessboard.push_back(i);
        }
        func(0, n);
        return ans;
    }
};      

37. 解数独

LeetCode N皇后 解数独(回溯算法)
LeetCode N皇后 解数独(回溯算法)
class Solution {
public:
    // 检查在board[row][col]的位置放val是否合法
    bool isValid(int row, int col, char val, const vector<vector<char>>& board){
        // 检查列
        for(int i = 0; i < 9; i++){
            if(board[i][col] == val){
                return false;
            }
        }
        // 检查行
        for(int j = 0; j < 9; j++){
            if(board[row][j] == val){
                return false;
            }
        }
        // 检查board[row][col]所在的3x3宫格
        int startRow = (row / 3) * 3;
        int startCol = (col / 3) * 3;
        for(int i = startRow; i < startRow + 3; i++){
            for(int j = startCol; j < startCol + 3; j++){
                if(board[i][j] == val){
                    return false;
                }
            }
        }
        return true;
    }

    bool func(vector<vector<char>>& board){
        // 这里从0开始...
        for(int i = 0; i < 9; i++){
            for(int j = 0; j < 9; j++){
                if(board[i][j] != '.'){
                    // board[i][j]已经被填写了
                    continue;
                }
                // 开始在board[i][j]位置尝试放置数字
                for(char val = '1'; val <= '9'; val++){
                    if(!isValid(i, j, val, board)){
                        continue;
                    }
                    board[i][j] = val;
                    if(func(board)){
                        // 找到合法的解,返回即可,不用再回溯了
                        return true;
                    }
                    board[i][j] = '.';
                }
                // 9个数字都不能放在board[i][j]位置,当前分支失败,需要向上回溯
                return false;
            }
        }
        return true;
    }


    void solveSudoku(vector<vector<char>>& board) {
        func(board);
    }
};