-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku_solver.py
61 lines (47 loc) · 1.33 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
from valid_sudoku import valid
ROWS =9
COLUMNS =9
board = [
[7,8,0,4,0,0,1,2,0],
[6,0,0,0,7,5,0,0,9],
[0,0,0,6,0,1,0,7,8],
[0,0,7,0,4,0,2,6,0],
[0,0,1,0,5,0,9,3,0],
[9,0,4,0,6,0,0,0,5],
[0,7,0,3,0,0,0,1,2],
[1,2,0,0,0,7,4,0,0],
[0,4,9,2,0,6,0,0,7]
]
def solve (board):
empty = find_empty (board)
if not empty:
return True
row, col = empty
for num in range (1,10):
if valid (board, num, empty):
board[row][col] = num
if solve (board):
return True
board[row][col] = 0
return False
def find_empty (board):
for row in range (ROWS):
for col in range (COLUMNS):
if board[row][col] == 0:
return (row, col)
return None
def print_board (board):
for row in range (ROWS):
if row % 3 == 0 and row != 0:
print ("- - - - - - - - - - - - - -")
for col in range (COLUMNS):
if col % 3 == 0 and col != 0:
print (" | ", end = " ")
if col == 8:
print (board[row][col])
else:
print (str (board[row][col]) + " ", end = "")
print_board (board)
solve (board)
print ("\n--------------------------------\n")
print_board (board)