-
Notifications
You must be signed in to change notification settings - Fork 1
/
SolveSudoku.py
61 lines (46 loc) · 1.24 KB
/
SolveSudoku.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
def Solve(grid):
locate = FindEmptyLocation(grid)
if locate:
row, col = locate
else:
return True
for i in range(1, 10):
if isSafe(grid, row, col, i):
grid[row][col] = i
if Solve(grid):
return True
grid[row][col] = 0
return False
def FindEmptyLocation(grid):
"""
To check if there is any empty cell in the board.
If present return the row and col of that cell
otherwise return None.
"""
for i in range(9):
for j in range(9):
if grid[i][j] == 0:
return i, j
return None
def isSafeRow(grid, row, x):
for i in range(9):
if grid[row][i] == x:
return False
return True
def isSafeCol(grid, col, x):
for i in range(9):
if grid[i][col] == x:
return False
return True
def isSafeBox(grid, row, col, x):
r = row - (row % 3)
c = col - (col % 3)
for i in range(3):
for j in range(3):
if grid[i + r][j + c] == x:
return False
return True
def isSafe(grid, row, col, x):
if isSafeRow(grid, row, x) and isSafeCol(grid, col, x) and isSafeBox(grid, row, col, x):
return True
return False