Skip to content

Commit ed63539

Browse files
Create Sudoku_Solver.py
1 parent 26dff43 commit ed63539

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

Sudoku_Solver.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Size of the 2D matrix (N x N)
2+
# grid_size = N / means N = 9
3+
grid_size = 9
4+
5+
# A utility function to print the grid
6+
def print_grid(grid):
7+
for row in range(grid_size):
8+
for col in range(grid_size):
9+
print(grid[row][col], end=" ")
10+
print()
11+
12+
# Checks whether it will be legal to assign num to the given row, col
13+
def is_safe(grid, row, col, num):
14+
# Check if we find the same num in the same row, return False
15+
for check_col in range(grid_size):
16+
if grid[row][check_col] == num:
17+
return False
18+
19+
# Check if we find the same num in the same column, return False
20+
for check_row in range(grid_size):
21+
if grid[check_row][col] == num:
22+
return False
23+
24+
# Check if we find the same num in the particular 3x3 matrix, return False
25+
start_row = row - row % 3
26+
start_col = col - col % 3
27+
for check_row in range(3):
28+
for check_col in range(3):
29+
if grid[check_row + start_row][check_col + start_col] == num:
30+
return False
31+
return True
32+
33+
# Takes a partially filled-in grid and attempts to assign values to all unassigned locations
34+
def solve_sudoku(grid, row, col):
35+
# Check if we have reached the last row and column, return True to avoid further backtracking
36+
if row == grid_size - 1 and col == grid_size:
37+
return True
38+
39+
# Check if column value becomes grid_size, move to the next row and column start from 0
40+
if col == grid_size:
41+
row += 1
42+
col = 0
43+
44+
# Check if the current position of the grid already contains a value > 0, iterate for the next column
45+
if grid[row][col] > 0:
46+
return solve_sudoku(grid, row, col + 1)
47+
48+
for num in range(1, grid_size + 1):
49+
# Check if it is safe to place the num (1-9) in the given row, col
50+
if is_safe(grid, row, col, num):
51+
# Assign the num in the current (row, col) position of the grid and assume it is correct
52+
grid[row][col] = num
53+
54+
# Check for the next possibility with the next column
55+
if solve_sudoku(grid, row, col + 1):
56+
return True
57+
58+
# If the assumption was wrong, remove the assigned num and go for the next assumption with a different num value
59+
grid[row][col] = 0
60+
61+
return False
62+
63+
# Driver Code
64+
# 0 means unassigned cells
65+
grid = [
66+
[3, 0, 6, 5, 0, 8, 4, 0, 0],
67+
[5, 2, 0, 0, 0, 0, 0, 0, 0],
68+
[0, 8, 7, 0, 0, 0, 0, 3, 1],
69+
[0, 0, 3, 0, 1, 0, 0, 8, 0],
70+
[9, 0, 0, 8, 6, 3, 0, 0, 5],
71+
[0, 5, 0, 0, 9, 0, 6, 0, 0],
72+
[1, 3, 0, 0, 0, 0, 2, 5, 0],
73+
[0, 0, 0, 0, 0, 0, 0, 7, 4],
74+
[0, 0, 5, 2, 0, 6, 3, 0, 0]
75+
]
76+
77+
if solve_sudoku(grid, 0, 0):
78+
print_grid(grid)
79+
else:
80+
print("No solution exists.")
81+
82+
#----------------------------------------------------------------------------------------------------------------------------------------------------------
83+
84+
# Answer: 3 1 6 5 7 8 4 9 2
85+
# 5 2 9 1 3 4 7 6 8
86+
# 4 8 7 6 2 9 5 3 1
87+
# 2 6 3 4 1 5 9 8 7
88+
# 9 7 4 8 6 3 1 2 5
89+
# 8 5 1 7 9 2 6 4 3
90+
# 1 3 8 9 4 7 2 5 6
91+
# 6 9 2 3 5 1 8 7 4
92+
# 7 4 5 2 8 6 3 1 9

0 commit comments

Comments
 (0)