-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathSudoku Solver
More file actions
27 lines (25 loc) · 1019 Bytes
/
Sudoku Solver
File metadata and controls
27 lines (25 loc) · 1019 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
class Solution:
def solveSudoku(self, board):
def solve():
for row in range(9):
for col in range(9):
if board[row][col] == '.':
for num in '123456789':
if is_valid(row, col, num):
board[row][col] = num
if solve():
return True
board[row][col] = '.'
return False
return True
def is_valid(row, col, num):
for i in range(9):
if board[row][i] == num or board[i][col] == num:
return False
start_row, start_col = 3 * (row // 3), 3 * (col // 3)
for i in range(start_row, start_row + 3):
for j in range(start_col, start_col + 3):
if board[i][j] == num:
return False
return True
solve()