-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathsudokusolver.py
75 lines (56 loc) · 2.09 KB
/
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
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
def next_empty_space(puzzle):
#finds the next row, col that is empty ;; returns (row,col) or ((None,None) if empty)
for r in range(9):
for c in range(9):
if puzzle[r][c]==-1:
return r,c
return None, None #if no space is empty
def is_valid(puzzle,guess,row,col):
#checks the guess at row of the puzzle is valid
row_vals= puzzle[row]
if guess in row_vals:
return False
#checks the guess at col of puzzle is valid
col_vals=[puzzle[i][col] for i in range(9)]
if guess in col_vals:
return False
#checks in 3X3 matrix
row_start= (row//3)*3
col_start= (col//3)*3
for r in range(row_start,row_start+3):
for c in range(col_start,col_start+3):
if puzzle[r][c]==guess:
return False
return True
def solve_sudoku(puzzle):
#solve sudoku using backtracking
#puzzle is a list of lists where each row is the inner list
row, col= next_empty_space(puzzle)
#when theres no free space left that means our puzzle is complete
if row is None:
return True
#if theres a free space then guess between 1 to 9
for guess in range(1,10):
if is_valid(puzzle,guess,row,col): #check if the guess is valid
puzzle[row][col]=guess
#recursively calls the function
if solve_sudoku(puzzle):
return True
#if not valid then we need to backtrack and try a new number
puzzle[row][col]= -1
#if this puzzle is unsolvable
return False
if __name__ == '__main__':
example_board= [
[-1,-1,-1, -1,-1,-1, 2,-1,-1],
[-1,8,-1, -1,-1,7, -1,9,-1],
[6,-1,2, -1,-1,-1, 5,-1,-1],
[-1,7,-1, -1,6,-1, -1,-1,-1],
[-1,-1,-1, 9,-1,1, -1,-1,-1],
[-1,-1,-1, -1,2,-1, -1,4,-1],
[-1,-1,5, -1,-1,-1, 6,-1,3],
[-1,9,-1, 4,-1,-1, -1,7,-1],
[-1,-1,6, -1,-1,-1, -1,-1,-1]
]
print(solve_sudoku(example_board))
print(example_board)