-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku_solver.py
79 lines (63 loc) · 1.88 KB
/
sudoku_solver.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
from tabulate import tabulate
# A 9x9 matrix which represents our sudoku solver
sudoku_board = [
[5,0,0,0,0,0,0,0,0],
[0,0,3,6,0,0,0,0,0],
[0,7,0,0,9,0,2,0,0],
[0,5,0,0,0,7,0,0,0],
[0,0,0,0,4,5,7,0,0],
[0,0,0,1,0,0,0,3,0],
[0,0,1,0,0,0,0,6,8],
[0,0,8,5,0,0,0,1,0],
[0,9,0,0,0,0,4,0,0]
]
# Display the board
def display_board(sudoku_board):
print(tabulate(sudoku_board, tablefmt='fancy_grid'))
#Look for an unassigned cell if it exists return row and col values else return False
def empty_cells_exist():
for i in range(9):
for j in range(9):
if sudoku_board[i][j] == 0:
return [i, j]
return False
# Is our choice good or not?
def valid_number_check(num, i, j):
#Checking vertically
for row in range(9):
if sudoku_board[row][j] == num:
return False
#Checking horizontally
for col in range(9):
if sudoku_board[i][col] == num:
return False
#Check in the 3x3 gird / box
grid_row = (i // 3) * 3
grid_col = (j // 3) * 3
for i in range(3):
for j in range(3):
if sudoku_board[grid_row + i][grid_col + j] == num:
return False
# Once all tests are passed return true
return True
# Solver
def solver():
cells_exist = empty_cells_exist()
if not cells_exist:
return True
i, j = cells_exist[0], cells_exist[1]
for num in range(1,10):
if valid_number_check(num, i, j):
sudoku_board[i][j] = num
#Backtracking (checking the next step)
if solver():
return True
else:
sudoku_board[i][j] = 0
# False if nothing (1 through 9) yield an "accepted answer"
return False
display_board(sudoku_board)
if solver():
display_board(sudoku_board)
else:
print("no solution available")