-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsudoku_solver_game.py
More file actions
74 lines (62 loc) · 2.5 KB
/
Copy pathsudoku_solver_game.py
File metadata and controls
74 lines (62 loc) · 2.5 KB
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
74
# Function to print the Sudoku grid
def print_board(board):
for row in board:
print(" ".join(str(num) if num != 0 else "." for num in row))
# Function to check if placing num in (row, col) is valid
def is_valid(board, num, row, col):
# Check if num is in the current row
if num in board[row]:
return False
# Check if num is in the current column
for i in range(9):
if board[i][col] == num:
return False
# Check if num is in the current 3x3 box
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
# Function to solve the Sudoku puzzle using backtracking
def solve_sudoku(board):
# Find the next empty spot (denoted by 0)
for row in range(9):
for col in range(9):
if board[row][col] == 0:
# Try possible numbers from 1 to 9
for num in range(1, 10):
if is_valid(board, num, row, col):
board[row][col] = num
# Recursively try to solve with this number placed
if solve_sudoku(board):
return True
# If placing num doesn't work, reset the spot
board[row][col] = 0
return False # No valid number was found, backtrack
return True # All cells are filled, puzzle is solved
# Function to solve and print the Sudoku board
def main():
# Example Sudoku board (0 represents empty cells)
board = [
[5, 3, 0, 0, 7, 0, 0, 0, 0],
[6, 0, 0, 1, 9, 5, 0, 0, 0],
[0, 9, 8, 0, 0, 0, 0, 6, 0],
[8, 0, 0, 0, 6, 0, 0, 0, 3],
[4, 0, 0, 8, 0, 3, 0, 0, 1],
[7, 0, 0, 0, 2, 0, 0, 0, 6],
[0, 6, 0, 0, 0, 0, 2, 8, 0],
[0, 0, 0, 4, 1, 9, 0, 0, 5],
[0, 0, 0, 0, 8, 0, 0, 7, 9]
]
print("Original Sudoku Board:")
print_board(board)
# Solve the Sudoku puzzle
if solve_sudoku(board):
print("\nSolved Sudoku Board:")
print_board(board)
else:
print("\nNo solution exists for this Sudoku puzzle.")
# Run the main function to start the solver
if __name__ == "__main__":
main()