Sudoku
Sudoku
Write a program to solve a Sudoku puzzle by filling the empty cells.
A sudoku solution must satisfyall of the following rules:
Each of the digits
1-9must occur exactly once in each row.Each of the digits
1-9must occur exactly once in each column.Each of the the digits
1-9must occur exactly once in each of the 93x3sub-boxes of the grid.
Empty cells are indicated by the character'.'.
Example
Example 1:
Input:
[
["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:
trueExample 2:
A sudoku puzzle...
...and its solution numbers marked in red.
Note:
The given board contain only digits
1-9and the character'.'.You may assume that the given Sudoku puzzle will have a single unique solution.
The given board size is always
9x9
Note
Judge is Valid - set version: Time O(9*9) space O(3*9)
Judge is Valid - trick version 不推荐:
Solver:
暴力去添加1到9,如果合适(valid)就继续dfs,search函数是boolean,不行就改回去,都不行这一层就是false
Code
Last updated