forked from davidxk/Sudoku-CSP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBacktrackSudokuSolver.py
39 lines (36 loc) · 1.4 KB
/
BacktrackSudokuSolver.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
from SudokuSolver import SudokuSolver
class BacktrackSudokuSolver(SudokuSolver):
def solveSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: void Do not return anything, modify board in-place instead.
"""
heng = [set() for i in range(9)]
zong = [set() for i in range(9)]
gezi = [set() for i in range(9)]
blank = []
for i in range(9):
for j in range(9):
if board[i][j] != '.':
k = i // 3 * 3 + j // 3
num = ord(board[i][j]) - ord('0') - 1
heng[i].add(num); zong[j].add(num); gezi[k].add(num)
else:
blank.append((i, j))
def backtrack(start):
if start >= len(blank):
return True
i, j = blank[start]
k = i // 3 * 3 + j // 3
for num in range(9):
if num not in heng[i] and num not in zong[j] and\
num not in gezi[k]:
board[i][j] = chr(ord('0') + num + 1)
heng[i].add(num); zong[j].add(num); gezi[k].add(num)
if backtrack(start + 1):
return True
heng[i].remove(num); zong[j].remove(num); gezi[k].remove(num)
board[i][j] = '.'
return False
backtrack(0)
return None