-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku_solution.py
129 lines (110 loc) · 4.95 KB
/
sudoku_solution.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from collections import defaultdict, deque
class Solution:
def solveSudoku(self, board) -> None:
"""
Do not return anything, modify board in-place instead.
"""
m, n = len(board), len(board[0])
def compileChoices(board, choices, decision_points):
rs = [set() for i in range(9)]
cs = [set() for i in range(9)]
ss = defaultdict(lambda: set())
to_decide = 0
for i in range(9):
for j in range(9):
if board[i][j] != 0:
rs[i].add(board[i][j])
cs[j].add(board[i][j])
si = i // 3
sj = j // 3
ss[(si, sj)].add(board[i][j])
for i in range(9):
for j in range(9):
if board[i][j] != 0: continue
choices[i][j] = set([x for x in range(1,10)])
choices[i][j] -= rs[i]
choices[i][j] -= cs[j]
choices[i][j] -= ss[(i//3, j//3)]
# print(i, j, choices[i][j])
if len(choices[i][j]) == 1:
decision_points.append((i, j))
to_decide += 1
elif len(choices[i][j]) > 1:
to_decide += 1
else:
# print(i, j, choices[i][j], "no choice?")
return -1
return to_decide
def updateChoices(board, choices, decision_points, to_decide):
while decision_points:
r, c = decision_points.pop()
if len(choices[r][c]) == 0:
if board[r][c] == 0: return -1
else: continue
board[r][c] = choices[r][c].pop()
to_decide -= 1
if to_decide == 0: return 0
for i in range(9):
if i == r: continue
if board[i][c] != 0: continue
if board[r][c] in choices[i][c]: choices[i][c].remove(board[r][c])
if len(choices[i][c]) == 1:
decision_points.append((i, c))
elif len(choices[i][c]) == 0:
return -1
for j in range(9):
if j == c: continue
if board[r][j] != 0: continue
if board[r][c] in choices[r][j]: choices[r][j].remove(board[r][c])
if len(choices[r][j]) == 1:
decision_points.append((r, j))
elif len(choices[r][j]) == 0:
return -1
for si in range(3):
for sj in range(3):
i = r // 3 * 3 + si
j = c // 3 * 3 + sj
if i == r: continue
if j == c: continue
if board[i][j] != 0: continue
if board[r][c] in choices[i][j]: choices[i][j].remove(board[r][c])
if len(choices[i][j]) == 1:
decision_points.append((i, j))
elif len(choices[i][j]) == 0:
return -1
return to_decide
def copyBoard(bd1, bd2):
for i in range(m):
for j in range(n):
bd1[i][j] = bd2[i][j]
def copyChoices(c1, c2):
for r in range(9):
for c in range(9):
c1[r][c] = c2[r][c].copy()
def getPos(cs):
for r in range(9):
for c in range(9):
if len(cs[r][c]): return r, c
choices = [ [set() for i in range(10) ] for j in range(10)]
decision_points = []
to_decide = compileChoices(board, choices, decision_points)
brd_states = deque([(board, choices, decision_points, to_decide)])
while brd_states:
bd, cs, ds, td = brd_states.popleft()
rv = updateChoices(bd, cs, ds, td)
if rv == 0:
print("Succeeded")
copyBoard(board, bd)
return
if rv == -1:
continue
nxt_td = rv
r, c = getPos(cs)
for choice in cs[r][c]:
nxt_bd = [[0]*9 for i in range(9)]
copyBoard(nxt_bd, bd)
nxt_cs = nxt_cs = [ [set() for i in range(10) ] for j in range(10)]
copyChoices(nxt_cs, cs)
nxt_cs[r][c] = {choice}
nxt_ds = [(r, c)]
brd_states.append((nxt_bd, nxt_cs, nxt_ds, nxt_td))