Project Overview
This project is a simple Sudoku Solver implemented in C++. It utilizes a backtracking algorithm to find a solution for any given 9x9 Sudoku puzzle. The program checks for valid placements of numbers in the grid and systematically explores possibilities until the puzzle is solved.
Features ->Backtracking Algorithm: The core of the solution is based on recursive backtracking. It tries placing a number in each empty cell and checks if the placement is valid. ->Grid Validation: The solver ensures that a number can be placed safely in a cell by checking the row, column, and 3x3 subgrid constraints. ->Input/Output: The program reads a partially filled Sudoku grid and prints the solved grid if a solution exists.
File Structure ->sudoku_solver.cpp: The main C++ file containing the Sudoku solver logic and a sample puzzle input.
How It Works ->Input: The Sudoku puzzle is represented by a 2D array (grid[9][9]), where 0 represents an empty cell. The input puzzle is hardcoded in the main() function.
Solver:
The function solveSudoku(grid) tries to solve the puzzle using a recursive backtracking approach. It first finds an empty cell using findEmptyLocation(). It then tries placing digits (1-9) into the empty cell while ensuring that no rules of Sudoku are violated. This check is performed by the isSafe() function. If the placement of a number is valid, the function proceeds recursively to solve the rest of the puzzle. If a solution is not possible, it backtracks by resetting the cell to 0 and tries the next possible number. Output: If the puzzle is solved successfully, the function prints the solved Sudoku grid. Otherwise, it indicates that no solution exists.
Code Breakdown Key Functions bool isSafe(int grid[N][N], int row, int col, int num) Checks if it's safe to place num in the cell located at (row, col) by ensuring the number is not already present in the current row, column, or 3x3 subgrid.
bool findEmptyLocation(int grid[N][N], int &row, int &col) Finds the next empty cell in the grid and updates the row and column accordingly.
bool solveSudoku(int grid[N][N]) The recursive function that attempts to solve the Sudoku puzzle using backtracking.
void printGrid(int grid[N][N]) Prints the current state of the Sudoku grid.