-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathSudokuSolver.py
More file actions
68 lines (49 loc) · 1.84 KB
/
Copy pathSudokuSolver.py
File metadata and controls
68 lines (49 loc) · 1.84 KB
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
class Solution:
# make a set of possible characters
possible = set(str(nr) for nr in range(1,10))
def solveSudoku(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
# save the board
self.board = board
self.dfs()
def dfs(self):
# find the next empty cell
rx, cx, complete = self.findEmpty()
# check whether there are empty cells
if complete:
return True
# get the available numbers
available = self.getPossible(rx, cx)
# check whether we have available numbers
if not available:
return False
# go through available numbers and try them
for number in available:
# set the number
self.board[rx][cx] = number
# check whether it works
if self.dfs():
return True
# reset the number
self.board[rx][cx] = "."
return False
def findEmpty(self):
# find the next free position and if all are filled we can return True
for rx in range(len(self.board)):
for cx in range(len(self.board[0])):
if self.board[rx][cx] == '.':
return rx, cx, False
return -1, -1, True
def getPossible(self, rx, cx):
return self.possible - self.getBox(rx, cx) - self.getRow(rx, cx) - self.getCol(rx, cx)
def getRow(self, rx, cx):
return set(self.board[rx])
def getCol(self, rx, cx):
return set([row[cx] for row in self.board])
def getBox(self, rx, cx):
result = set()
for rx in range(rx // 3 * 3, rx // 3 * 3 + 3):
result.update(self.board[rx][cx // 3 * 3:cx // 3 * 3 + 3])
return result