https://github.com/vish501/sudoku-solver
Sudoku Solver using Backtracking and Recursion
https://github.com/vish501/sudoku-solver
backtracking recursion sudoku sudoku-solver
Last synced: about 2 months ago
JSON representation
Sudoku Solver using Backtracking and Recursion
- Host: GitHub
- URL: https://github.com/vish501/sudoku-solver
- Owner: Vish501
- Created: 2023-12-28T11:56:03.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-12-05T09:16:16.000Z (5 months ago)
- Last Synced: 2025-03-19T05:44:19.722Z (about 2 months ago)
- Topics: backtracking, recursion, sudoku, sudoku-solver
- Language: Python
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Sudoku Solver (Backtracking and Recursion)
This Python program utilizes backtracking and recursion to solve Sudoku puzzles.
**How it Works:**
1. **Initialization:**
- Takes a 9x9 Sudoku grid as input (represented as a list of lists).
- Empty cells are represented by -1.2. **Find Empty Cell:**
- A helper function efficiently locates the next empty cell in the grid.3. **Backtracking Algorithm:**
- If no empty cells are found, the current grid is a valid solution, and the program returns True.
- For each possible number (1-9):
- Place the number in the empty cell.
- Recursively call the solver to check if the updated grid is valid.
- If the recursive call returns True, the current grid is solved, and the program returns True.
- If the recursive call returns False, remove the number from the cell and try another number.
- If no number leads to a valid solution, backtrack and try different numbers in previous empty cells.4. **Validation:**
- Checks if a number is valid in a specific cell by ensuring it doesn't violate Sudoku rules:
- No duplicates in the same row.
- No duplicates in the same column.
- No duplicates in the same 3x3 subgrid.**Usage:**
1. **Prepare Input:**
- Create a 9x9 list of lists representing the Sudoku grid.
- Use -1 to represent empty cells.2. **Run the Program:**
- Call the `solve_sudoku()` function with the input grid.3. **Output:**
- Sudoku grid solved, in place.**Example:**
```python
grid = [
[3, 9, -1, -1, 5, -1, -1, -1, -1],
[-1, -1, -1, 2, -1, -1, -1, -1, 5],
[-1, -1, -1, 7, 1, 9, -1, 8, -1],[-1, 5, -1, -1, 6, 8, -1, -1, -1],
[2, -1, 6, -1, -1, 3, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, 4],[5, -1, -1, -1, -1, -1, -1, -1, -1],
[6, 7, -1, 1, -1, 5, -1, 4, -1],
[1, -1, 9, -1, -1, -1, 2, -1, -1]
]
solve_sudoku(puzzle)
print(puzzle)