-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudokuSolver.py
36 lines (30 loc) · 874 Bytes
/
sudokuSolver.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
from Board import Board
def solve(board, cell_index=0):
if cell_index == 81:
return True
row = cell_index // 9
column = cell_index % 9
if board.isFilled(row, column):
return solve(board, cell_index+1)
possible_cell_values = board.getPossibleCellValues(row, column)
for value in possible_cell_values:
board.setCell(row, column, value)
if solve(board, cell_index+1):
return True
board.setCell(row, column, 0)
return False
if __name__ == "__main__":
test_board = [
[7,0,2,6,0,0,0,0,9],
[0,0,0,0,1,2,0,0,0],
[0,3,0,8,0,0,0,6,0],
[0,0,7,0,0,5,4,0,0],
[0,0,0,2,7,8,0,0,0],
[0,0,5,3,0,0,7,0,0],
[0,9,0,0,0,3,0,5,0],
[0,0,0,9,8,0,0,0,0],
[3,0,0,0,0,6,9,0,8]]
b = Board(test_board)
print(b)
solve(b)
print(b)