-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.py
More file actions
29 lines (25 loc) · 839 Bytes
/
Copy pathsolver.py
File metadata and controls
29 lines (25 loc) · 839 Bytes
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
def is_valid(board, row, col, num):
for x in range(9):
if board[row][x] == num:
return False
if board[x][col] == num:
return False
box_x = col // 3 * 3
box_y = row // 3 * 3
for i in range(3):
for j in range(3):
if board[box_y + i][box_x + j] == num:
return False
return True
def solve_sudoku(board):
for row in range(9):
for col in range(9):
if board[row][col] == 0:
for num in range(1, 10):
if is_valid(board, row, col, num):
board[row][col] = num
if solve_sudoku(board):
return True
board[row][col] = 0
return False
return True