-
Notifications
You must be signed in to change notification settings - Fork 481
/
0037.py
75 lines (64 loc) · 2.49 KB
/
0037.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class Solution:
def solveSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: void Do not return anything, modify board in-place instead.
"""
colnum = []
rownum = []
sqnum = [[0] * 3 for _ in range(3)]
for i in range(9):
colnum.append(set(tuple(range(1, 10))))
rownum.append(set(tuple(range(1, 10))))
for i in range(3):
for j in range(3):
sqnum[i][j] = set(tuple(range(1, 10)))
empty = set()
for i in range(9):
for j in range(9):
if board[i][j] != '.':
int_element = int(board[i][j])
rownum[i].remove(int_element)
colnum[j].remove(int_element)
sqnum[i//3][j//3].remove(int_element)
else:
empty.add((i,j))
self.DFS(empty, colnum, rownum, sqnum, board)
def DFS(self, empty, colnum, rownum, sqnum, board):
if len(empty) == 0:
return True
max_possible_len = 9
for (r, c) in empty:
possible = rownum[r] & colnum[c] & sqnum[r//3][c//3]
if len(possible) <= max_possible_len:
max_possible_len = len(possible)
i, j = r, c
if max_possible_len == 0:
return False
empty.remove((i, j))
possible = rownum[i] & colnum[j] & sqnum[i//3][j//3]
for num in possible:
rownum[i].remove(num)
colnum[j].remove(num)
sqnum[i//3][j//3].remove(num)
board[i][j] = str(num)
if self.DFS(empty, colnum, rownum, sqnum, board):
return True
rownum[i].add(num)
colnum[j].add(num)
sqnum[i//3][j//3].add(num)
board[i][j] = "."
empty.add((i, j))
return False
if __name__ == "__main__":
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"]]
Solution().solveSudoku(board)
print(board)