0% found this document useful (0 votes)
18 views3 pages

Sudoku Game Implementation in C

The document describes a C program to implement a Sudoku solver. It includes functions to print the grid, find unassigned locations, check if a number can be placed safely in a position, recursively solve the puzzle by trying numbers, and a main function that tests the solver on a sample partially filled grid. The program successfully solves the sample Sudoku puzzle and prints the completed grid.

Uploaded by

71762108021
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

Sudoku Game Implementation in C

The document describes a C program to implement a Sudoku solver. It includes functions to print the grid, find unassigned locations, check if a number can be placed safely in a position, recursively solve the puzzle by trying numbers, and a main function that tests the solver on a sample partially filled grid. The program successfully solves the sample Sudoku puzzle and prints the completed grid.

Uploaded by

71762108021
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Ex.

No: 1
SUDOKU
Date: 16.8.23

Aim:
To implement Sudoku game in C language.

Program Code:

#include <stdio.h>

#define N 9

// Function to print the Sudoku grid


void printGrid(int grid[N][N]) {
for (int row = 0; row < N; row++) {
for (int col = 0; col < N; col++) {
printf("%d ", grid[row][col]);
}
printf("\n");
}
}

// Function to find an unassigned cell in the grid


int findUnassignedLocation(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 1; // Unassigned cell found
}
}
}
return 0; // No unassigned cell found
}

// Function to check if a number can be placed at the given position


int isSafe(int grid[N][N], int row, int col, int num) {
// Check row and column
for (int x = 0; x < N; x++) {
if (grid[row][x] == num || grid[x][col] == num) {
return 0;

1
}
}

// Check 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[startRow + i][startCol + j] == num) {
return 0;
}
}
}

return 1;
}

// Function to solve the Sudoku puzzle


int solveSudoku(int grid[N][N]) {
int row, col;
if (!findUnassignedLocation(grid, &row, &col)) {
return 1; // All cells are assigned, puzzle solved
}

for (int num = 1; num <= 9; num++) {


if (isSafe(grid, row, col, num)) {
grid[row][col] = num;
if (solveSudoku(grid)) {
return 1; // If successful, puzzle solved
}
grid[row][col] = 0; // If not successful, backtrack
}
}

return 0; // No number can be placed, backtrack


}

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},

2
{0, 0, 0, 0, 8, 0, 0, 7, 9}
};

if (solveSudoku(grid)) {
printf("Sudoku solved:\n");
printGrid(grid);
} else {
printf("No solution exists.\n");
}

return 0;
}

Output:

Result:
The Sudoku game in C language is implemented successfully.

You might also like