36. Valid Sudoku / 37. Sudoku Solver

36. Valid Sudoku

如下图对方格进行划分,划分后将 box 的序号与 i, j 对应起来,即 box_index = ( i / 3 ) * 3 + ( j / 3 )

 

如此以来问题便简单了许多,只需要遍历一遍,对每个 i, j 判断是否有效即可,分为位判断哈希匹配两种,对于详细的位运算暂时还没进一步理解,写了一个比哈希匹配还 low 的强行位运算。

class Solution {
private:
    bool checkMap(unsigned int mp[], int i, int value){
        unsigned int binValue = (1 << (value - 1));
        if(mp[i] == 0){
            mp[i] |= binValue;
            return true;
        }
        if((binValue & mp[i]) != 0){
            return false;
        }
        mp[i] |= binValue;
        return true;
    }
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        int len = board.size();
        unsigned int row_map[len],col_map[len], box_map[len];
        for(int i = 0; i < len; i++){
            row_map[i] = col_map[i] = box_map[i] = 0b000000000; // 9 bit 0 of binary
        }
        for(int i = 0; i < len; i++){
            for(int j = 0; j < len; j++){
                if(board[i][j] == '.'){
                    continue;
                }
                if(!checkMap(row_map, i, board[i][j] - '0') || !checkMap(col_map, j, board[i][j] - '0') || !checkMap(box_map, (i / 3) * 3 + (j / 3), board[i][j] - '0')){
                    return false;
                }
            }
        }
        return true;
    }
};
在上道题36. Valid Sudoku的基础上,加上回溯法即可。
class Solution {
private:
    bool checkMap(unsigned int mp[], int i, int value){
        unsigned int binValue = (1 << (value - 1));
        if(mp[i] == 0){
            mp[i] |= binValue;
            return true;
        }
        if((binValue & mp[i]) != 0){
            return false;
        }
        mp[i] |= binValue;
        return true;
    }
    bool isValidSudoku(vector<vector<char>>& board) {
        int len = board.size();
        unsigned int row_map[len],col_map[len], box_map[len];
        for(int i = 0; i < len; i++){
            row_map[i] = col_map[i] = box_map[i] = 0b000000000; // 9 bit 0 of binary
        }
        for(int i = 0; i < len; i++){
            for(int j = 0; j < len; j++){
                if(board[i][j] == '.'){
                    continue;
                }
                if(!checkMap(row_map, i, board[i][j] - '0') || !checkMap(col_map, j, board[i][j] - '0') || !checkMap(box_map, (i / 3) * 3 + (j / 3), board[i][j] - '0')){
                    return false;
                }
            }
        }
        return true;
    }
    bool solve(vector<vector<char>>& board,int len, int s){
        int si = s / len, sj = s % len;
        if(s == len * len){
            return true;
        }
        if(board[si][sj] != '.'){
            return solve(board, len, s+1);
        }
        int next = s + 1;
        int nexti = next / len, nextj = next % len;
        for(int i = 1; i < 10; i++){
            board[si][sj] = '0' + i;
            if(isValidSudoku(board)){
                if(solve(board, len, next)){
                    return true;
                }
            }
            board[si][sj] = '.';
        }
        return false;
    }
public:
    void solveSudoku(vector<vector<char>>& board) {
        int len = board.size();
        solve(board, len, 0);
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值