diff options
-rw-r--r-- | sudoku_solver.c | 69 | ||||
-rw-r--r-- | sudoku_solver.py | 66 |
2 files changed, 135 insertions, 0 deletions
diff --git a/sudoku_solver.c b/sudoku_solver.c new file mode 100644 index 0000000..88ccc01 --- /dev/null +++ b/sudoku_solver.c @@ -0,0 +1,69 @@ +// 37. Sudoku Solver +#include "leetcode.h" + +/* + * 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, and 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. + */ + +bool solve(char **board, bool row[9][9], bool col[9][9], bool grid[9][9], int r, + int c) { + if (r == 9) + return true; + int next_r = c == 8 ? r + 1 : r; + int next_c = c == 8 ? 0 : c + 1; + if (board[r][c] != '.') + return solve(board, row, col, grid, next_r, next_c); + int grid_idx = (r / 3) * 3 + (c / 3); + for (int i = 0; i < 9; i++) { + if (!row[r][i] && !col[c][i] && !grid[grid_idx][i]) { + board[r][c] = '1' + i; + row[r][i] = col[c][i] = grid[grid_idx][i] = true; + if (solve(board, row, col, grid, next_r, next_c)) + return true; + board[r][c] = '.'; + row[r][i] = col[c][i] = grid[grid_idx][i] = false; + } + } + return false; +} + +void solveSudoku(char **board, int boardSize, int *boardColSize) { + if (boardSize != 9 || *boardColSize != 9) + return; + bool row[9][9] = {false}; + bool col[9][9] = {false}; + bool grid[9][9] = {false}; + for (int i = 0; i < 9; i++) + for (int j = 0; j < 9; j++) + if (board[i][j] != '.') { + int num = board[i][j] - '1'; + if (num < 0 || num >= 9) + return; + row[i][num] = col[j][num] = true; + grid[(i / 3) * 3 + (j / 3)][num] = true; + } + solve(board, row, col, grid, 0, 0); +} + +int main() { + char *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"}}; + solveSudoku((char **)board, ARRAY_SIZE(board), ARRAY_SIZE(board[0])); + for (int i = 0; i < ARRAY_SIZE(board); i++) { + for (int j = 0; j < ARRAY_SIZE(board[0]); j++) + printf("%c ", board[i][j]); + printf("\n"); + } +} diff --git a/sudoku_solver.py b/sudoku_solver.py new file mode 100644 index 0000000..7a395e7 --- /dev/null +++ b/sudoku_solver.py @@ -0,0 +1,66 @@ +# 37. Sudoku Solver + +""" +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, and 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. +""" + + +class Solution(object): + def solveSudoku(self, board): + """ + :type board: List[List[str]] + :rtype: None Do not return anything, modify board in-place instead. + """ + n = 9 + + def valid(row, col, idx): + row, col = int(row), int(col) + for i in range(9): + if board[i][col] == idx: + return False + if board[row][i] == idx: + return False + if board[3 * (row // 3) + i // 3][3 * (col // 3) + i % 3] == idx: + return False + return True + + def solve(row, col): + if row == n: + return True + if col == n: + return solve(row + 1, 0) + if board[row][col] == ".": + for i in range(1, 10): + if valid(row, col, str(i)): + board[row][col] = str(i) + if solve(row, col + 1): + return True + else: + board[row][col] = "." + return False + else: + return solve(row, col + 1) + + solve(0, 0) + + +if __name__ == "__main__": + obj = Solution() + 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"], + ] + obj.solveSudoku(board) + print(board) |