36.有効な数独/ 37数独ソルバー

36.有効な数独

他のセルの下にI、Jの対応、すなわちbox_index =(I / 3)* 3 +(J / 3)の後に、分割数とボックスを分割

 

問題は簡単に多くのことをされるようにするので、ちょうどそれぞれのiについて、jが分かれ妥当性、決定するために、再び繰り返すために必要なビットの判断ハッシュ試合を 2種類の詳細なビットは、コンピューティング何の更なる理解が書いていないこと測位計算に一致BI Haxi力も低いです。

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.有効な数独は、上のベース、プラスバックトラックをすることができます。
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