-
Notifications
You must be signed in to change notification settings - Fork 3
/
SolveSudoku.py
73 lines (58 loc) · 1.8 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
''' Solving 9 X 9 sudoku puzzle using Backtracking method '''
def check_emptycell(puzzle):
for i in range(9):
for j in range(9):
if puzzle[i,j] == 0:
return [i,j]
return None
def check_row(puzzle,row,col,n):
for j in range(9):
if puzzle[row,j] == n:
return False
return True
def check_col(puzzle,row,col,n):
for i in range(9):
if puzzle[i,col] == n:
return False
return True
def check_box(puzzle,row,col,n):
for i in range(row - row%3, row - row%3 + 3):
for j in range(col - col%3, col - col%3 + 3):
if puzzle[i,j] == n:
return False
return True
def check_safe_cell(puzzle,row,col,n):
return (check_box(puzzle, row, col, n) and check_row(puzzle, row, col, n) and check_col(puzzle, row, col, n))
def solve_sudoku(grid):
loc=check_emptycell(grid)
if loc is None:
return True
for n in range(1,10):
if check_safe_cell(grid, loc[0], loc[1], n):
grid[loc[0],loc[1]]=n
if solve_sudoku(grid):
return True
grid[loc[0],loc[1]]=0
return False
def display_sudoku(board):
print("\n+---------------+---------------+---------------+")
for i in range(9):
print('|',end='\t')
for j in range(9):
if board[i,j]!=0:
print(board[i,j],end="\t")
else:
print(" ",end="\t")
if((j+1)%3==0):
print('|',end='\t')
if (i+1)%3==0:
print("\n+---------------+---------------+---------------+")
else:
print()
def main(sudoku):
if solve_sudoku(sudoku):
print("Solved successfully!")
else:
print("No solution exist...")
display_sudoku(sudoku)
return sudoku