-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboard_solver.py
37 lines (28 loc) · 902 Bytes
/
board_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
from config_validity_checker import currentconfig_isvalid
from empty_square_finder import find_empty
def solve(board):
empty_sq = find_empty(board)
if not empty_sq:
return True, board
else:
row, col = empty_sq
for i in range(1, 10):
if currentconfig_isvalid(board, i, (row, col)):
board[row][col] = i
if solve(board)[0]:
return True, board
board[row][col] = 0
return False, None
if __name__ == "__main__":
test_board = [
[0, 0, 0, 8, 0, 0, 4, 2, 0],
[5, 0, 0, 6, 7, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 9, 0, 0, 5],
[7, 4, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 9, 0, 3, 0, 7, 0, 0],
[0, 0, 0, 0, 0, 7, 0, 4, 8],
[8, 0, 0, 4, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 9, 8, 0, 0, 3],
[0, 9, 5, 0, 0, 3, 0, 0, 0,],
]
print(solve(test_board)[1])