美文网首页
LeetCode #37 Sudoku Solver

LeetCode #37 Sudoku Solver

作者: 刘煌旭 | 来源:发表于2021-04-01 23:57 被阅读0次
Write a program to solve a Sudoku puzzle by filling the empty cells.

A sudoku solution must satisfy all of the following rules:

Each of the digits 1-9 must occur exactly once in each row.
Each of the digits 1-9 must occur exactly once in each column.
Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.
The '.' character indicates empty cells.

 

Example 1:


Input: board = [["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]]
Output: [["5","3","4","6","7","8","9","1","2"],["6","7","2","1","9","5","3","4","8"],["1","9","8","3","4","2","5","6","7"],["8","5","9","7","6","1","4","2","3"],["4","2","6","8","5","3","7","9","1"],["7","1","3","9","2","4","8","5","6"],["9","6","1","5","3","7","2","8","4"],["2","8","7","4","1","9","6","3","5"],["3","4","5","2","8","6","1","7","9"]]
Explanation: The input board is shown above and the only valid solution is shown below:


 

Constraints:

board.length == 9
board[i].length == 9
board[i][j] is a digit or '.'.
It is guaranteed that the input board has only one solution.
/**
* Using backtrack to enumerate all possibilities...
*/
typedef struct {
    int i, j;
}*Location;

Location MakeLocation(int i, int j) {
    Location l = (Location)malloc(sizeof(*l));
    l->i = i;
    l->j = j;
    return l;
}

int* cal_legal(int mask[10], int rmask, int cmask, int gmask, size_t *size) {
    int *legal = (int*)malloc(9 * sizeof(*legal)), ptr = 0;
    for (int i = 1; i < 10; i++) { if (!(mask[i] & (rmask | cmask | gmask))) legal[ptr++] = i; }
    *size = ptr;
    return legal;
}

void solveSudoku(char** board, int boardSize, int* boardColSize){
    int mask[10] = {0}, rmask[9] = {0}, cmask[9] = {0}, gmask[9] = {0};

    for (int i = 1; i < 10; i++) { mask[i] = 1 << i; }
    
    int sc = 0, gindex[9][9];
    Location spaces[81];
    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            gindex[i][j] = i / 3 * 3 + j / 3;
            if (board[i][j] != '.') {
                int bit = 1 << (board[i][j] - '0');
                rmask[i] |= bit;
                cmask[j] |= bit;
                gmask[gindex[i][j]] |= bit;
            } else { spaces[sc++] = MakeLocation(i, j); }
        }
    }
    
    int *legal[sc], ptr[sc];
    size_t legalc[sc];
    for (int i = 0; i < sc; i++) {
        ptr[i] = 0;
        Location loc = spaces[i];
        legal[i] = cal_legal(mask, rmask[loc->i], cmask[loc->j], gmask[gindex[loc->i][loc->j]], legalc + i);
    }

    int i = 0, crmask[9] = {0}, ccmask[9] = {0}, cgmask[9] = {0};
    while (i < sc) {
        Location loc = spaces[i];
        if (ptr[i] < legalc[i]) {
            int fill = legal[i][ptr[i]++];
            if (!(mask[fill] & (crmask[loc->i] | ccmask[loc->j] | cgmask[gindex[loc->i][loc->j]]))) {
                crmask[loc->i] |= mask[fill];
                ccmask[loc->j] |= mask[fill];
                cgmask[gindex[loc->i][loc->j]] |= mask[fill];
                board[loc->i][loc->j] = fill + '0';
                i++;
            }
        } else {
            board[loc->i][loc->j] = '0';
            ptr[i] = 0;
            i--;
            loc = spaces[i];
            int fill = board[loc->i][loc->j] - '0';
            crmask[loc->i] &= ~(mask[fill]);
            ccmask[loc->j] &= ~(mask[fill]);
            cgmask[gindex[loc->i][loc->j]] &= ~(mask[fill]);
            board[loc->i][loc->j] = '0';
        }
    }
}

相关文章

网友评论

      本文标题:LeetCode #37 Sudoku Solver

      本文链接:https://www.haomeiwen.com/subject/dyxfkltx.html