回溯——LeetCode N皇后 解数独

51. 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. 解数独

在这里插入图片描述

在这里插入图片描述

图源

暴力法填写即可

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);
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bugcoder-9905

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值