Create a Sudoku Solver in C Language with Free Codes

Index

Sudoku Solver

Here You get free Codes of Sudoku Solver Project which is created in C Language. Sudoku Solver in C involves the combination of backtracking algorithm and techniques specific to Sudoku solving.

Sudoku Solver Formula

The Sudoku Solver Formula used in this program is based on a backtracking algorithm:

  1. The Program tries to place numbers from 1 to 9 in each empty cell of the grid.
  2. It checks if the placement is valid using the “isSafe” function.
  3. If a valid number can be placed, the program moves to the next empty cell and repeats the process.
  4. If a valid number cannot be placed, the program backtracks to the previous cell and tries a different number.
  5. This process continues until a solution is found or all possibilities are exhausted.
C Language E-Books

Sudoku Solver Codes

Here is the Complete Codes of Sudoku Solver,

#include <stdio.h>
#include <stdbool.h>

#define N 9

// Function to check if a number can be placed in the given cell
bool isSafe(int grid[N][N], int row, int col, int num) {
    // Check if the number exists in the current row or column
    for (int x = 0; x < N; x++) {
        if (grid[row][x] == num || grid[x][col] == num) {
            return false;
        }
    }

    // Check if the number exists in the current 3x3 subgrid
    int startRow = row - row % 3;
    int startCol = col - col % 3;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (grid[i + startRow][j + startCol] == num) {
                return false;
            }
        }
    }

    return true; // If the number can be placed in the cell
}

// Function to find an empty cell in the grid
bool findEmptyCell(int grid[N][N], int *row, int *col) {
    for (*row = 0; *row < N; (*row)++) {
        for (*col = 0; *col < N; (*col)++) {
            if (grid[*row][*col] == 0) {
                return true; // Found an empty cell
            }
        }
    }
    return false; // No empty cell found
}

// Function to solve the Sudoku puzzle using backtracking
bool solveSudoku(int grid[N][N]) {
    int row, col;

    // Find an empty cell in the grid
    if (!findEmptyCell(grid, &row, &col)) {
        return true; // If no empty cell, puzzle solved
    }

    // Try placing numbers 1 to 9 in the empty cell
    for (int num = 1; num <= 9; num++) {
        if (isSafe(grid, row, col, num)) {
            grid[row][col] = num; // Place the number

            // Recur to solve the rest of the puzzle
            if (solveSudoku(grid)) {
                return true; // If solution found, return true
            }

            grid[row][col] = 0; // If solution not found, backtrack
        }
    }

    return false; // No solution exists for this cell
}

// Function to print the solved Sudoku grid
void printGrid(int grid[N][N]) {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            printf("%2d ", grid[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int grid[N][N] = {
        {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}
    };

    printf("Sudoku Puzzle:\n");
    printGrid(grid);

    if (solveSudoku(grid)) {
        printf("\nSolved Sudoku:\n");
        printGrid(grid);
    } else {
        printf("\nNo solution exists for the given Sudoku puzzle.\n");
    }

    return 0;
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top