-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.py
94 lines (78 loc) · 2.18 KB
/
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
80
81
82
83
84
85
86
87
88
89
90
91
92
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]
]
# board = [
# [7, 8, 0],
# [6, 0, 0],
# [0, 0, 0]
# ]
#
# board = [
# [4, 6, 7, 0, 0, 5, 0, 0, 0],
# [0, 5, 0, 1, 0, 0, 7, 0, 0],
# [0, 0, 0, 0, 4, 7, 0, 3, 6],
# [9, 1, 0, 2, 0, 6, 0, 5, 0],
# [0, 0, 8, 4, 0, 9, 3, 0, 0],
# [0, 7, 0, 3, 0, 8, 0, 9, 2],
# [7, 9, 0, 5, 8, 0, 0, 0, 0],
# [0, 0, 2, 0, 0, 3, 0, 7, 0],
# [0, 0, 0, 7, 0, 0, 1, 6, 9]
# ]
def get_solution():
return solve_one_square(find_empty())
def is_valid(loc, num):
'''
:param loc: [i,j] where i,j represents board[i][j] (row, col)
:param num: input number
:return: a boolean representing whether or not the number can be entered
'''
row, col = loc
# check row
for j in range(len(board[row])):
if j!=col and board[row][j]==num:
return False
# check col
for i in range(len(board)):
if i!=row and board[i][col]==num:
return False
# check big square
big_row, big_col = row//3, col//3
for i in range(big_row*3, big_row*3+3):
for j in range(big_col*3, big_col*3+3):
if i!=row and j!=col and board[i][j]==num:
return False
return True
def find_empty():
for i in range(len(board)):
for j in range(len(board[0])):
if board[i][j] == 0:
return [i,j]
return None
def solve_one_square(loc):
if loc:
row,col = loc
find = False
for i in range(1,10):
print(f'{loc} - {i}')
if is_valid(loc, i):
board[row][col] = i
if solve_one_square(find_empty()):
return True
if find:
return True
else:
board[row][col] = 0
return False
else:
return True
if __name__=='__main__':
print(get_solution())
print(board)