forked from gavinfish/leetcode-share
-
Notifications
You must be signed in to change notification settings - Fork 0
/
037 Sudoku Solver.py
56 lines (44 loc) · 1.8 KB
/
037 Sudoku Solver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'''
Write a program to solve a Sudoku puzzle by filling the empty cells.
Empty cells are indicated by the character '.'.
You may assume that there will be only one unique solution.
A sudoku puzzle...
...and its solution numbers marked in red.
'''
class Solution(object):
def solveSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: void Do not return anything, modify board in-place instead.
"""
for row in range(9):
board[row] = list(board[row])
self.recursive(0, 0, board)
for row in range(9):
board[row] = "".join(board[row])
def recursive(self, i, j, board):
if j >= 9:
return self.recursive(i + 1, 0, board)
if i == 9:
return True
if board[i][j] == ".":
for num in range(1, 10):
num_str = str(num)
if all([board[i][col] != num_str for col in
range(9)]) and all([board[row][j] != num_str for row in range(9)]) and all(
[board[i // 3 * 3 + count // 3][j // 3 * 3 + count % 3] != num_str for count in
range(9)]):
board[i][j] = num_str
if not self.recursive(i, j + 1, board):
board[i][j] = "."
else:
return True
else:
return self.recursive(i, j + 1, board)
return False
if __name__ == "__main__":
sudoku = ["..9748...", "7........", ".2.1.9...", "..7...24.", ".64.1.59.", ".98...3..", "...8.3.2.", "........6",
"...2759.."]
Solution().solveSudoku(sudoku)
assert sudoku == ['519748632', '783652419', '426139875', '357986241', '264317598', '198524367', '975863124',
'832491756', '641275983']