Sudoku Solver
To solve this coding challenge of a Sudoku Solver, we need to create a program that can fill in the empty cells of a given 9x9 Sudoku board while adhering to the game's rules. Specifically, the rules state that each digit from 1 to 9 must appear exactly once in each row, column, and the three-by-three sub-boxes of the grid.
Explanation
Step-by-Step Logic:- Input Representation:
-
The board is a 2D list where each sub-list represents a row of the Sudoku. Empty cells are represented by the character
'.'
- Validation Function:
- We need a function to check if placing a particular number in a cell is valid. This involves three checks:
- Row Check: The number should not already exist in the current row.
- Column Check: The number should not already exist in the current column.
- Sub-box Check: The number should not already exist in the current 3x3 sub-box.
- The 3x3 sub-box can be determined using integer division and modulus operations.
- Recursive Backtracking:
- Use a recursive approach to attempt to place numbers in each empty cell. If placing the number leads to a valid solution, we proceed. If not, we backtrack and try the next number.
- For each cell, iterate through digits '1' to '9', and use the validation function to check if the digit placement is valid.
- Place the digit temporarily and recursively attempt to solve the remainder of the board.
- If hitting a dead-end, reset the cell and backtrack to try the next available number.
- End Condition:
- When there are no more empty cells without contradictions, the board is successfully solved.
Detailed Steps in Pseudocode
# Pseudocode for Sudoku Solver
# Function to determine if placing 'num' at board[row][col] is valid
function is_valid(board, row, col, num):
# Check if 'num' is already in the current row
for i from 0 to 8:
if board[row][i] == num:
return False
# Check if 'num' is already in the current column
for i from 0 to 8:
if board[i][col] == num:
return False
# Determine the starting indices of the 3x3 sub-box
startRow = (row // 3) * 3
startCol = (col // 3) * 3
# Check if 'num' is already in the 3x3 sub-box
for i from 0 to 2:
for j from 0 to 2:
if board[startRow + i][startCol + j] == num:
return False
# If all checks passed, it's valid to place 'num' at board[row][col]
return True
# Main function to solve the Sudoku board using recursion and backtracking
function solve_sudoku(board):
# Recursive helper function that attempts to solve the board
function solve():
# Iterate over each cell in the board
for i from 0 to 8:
for j from 0 to 8:
# If cell is empty, try placing each digit from '1' to '9'
if board[i][j] == '.':
for num in '123456789':
# Check if placing 'num' is valid
if is_valid(board, i, j, num):
# Temporarily place 'num' in the cell
board[i][j] = num
# Recursively attempt to solve the rest of the board
if solve():
return True
# Backtrack: revert the cell to empty if dead-end is reached
board[i][j] = '.'
# If no digits solve the board from this state, return False
return False
# If every cell is filled correctly, return True
return True
# Initiate the solving process
solve()
# Example usage
board = [["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]]
solve_sudoku(board)
# After running the function, the 'board' will be modified to the solved state.
By adhering to this step-by-step process and the respective pseudocode, we ensure the Sudoku grid is correctly filled, respecting all the rules intrinsic to a valid Sudoku solution. Recursive backtracking provides a straightforward method to explore all potential configurations systematically, guaranteeing a solution when feasible.