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);
    }
};
发布了62 篇原创文章 · 获赞 9 · 访问量 7788

猜你喜欢

转载自blog.csdn.net/qq_40491305/article/details/104154244