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

Sudoku Project Report Complete

This document contains a C program that implements a Sudoku solver using backtracking. It includes functions to check the validity of numbers in the Sudoku grid, solve the puzzle, and print the board. The program initializes a sample Sudoku puzzle and attempts to solve it, displaying the solved grid if successful.

Uploaded by

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

Sudoku Project Report Complete

This document contains a C program that implements a Sudoku solver using backtracking. It includes functions to check the validity of numbers in the Sudoku grid, solve the puzzle, and print the board. The program initializes a sample Sudoku puzzle and attempts to solve it, displaying the solved grid if successful.

Uploaded by

ahonasarkar63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Program Code

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 9

int board[N][N];

int isValid(int row, int col, int num) {


for (int x = 0; x < N; x++)
if (board[row][x] == num || board[x][col] == num)
return 0;

int startRow = row - row % 3, startCol = col - col % 3;


for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (board[i + startRow][j + startCol] == num)
return 0;

return 1;
}

int solveSudoku() {
int row, col, found = 0;
for (row = 0; row < N && !found; row++)
for (col = 0; col < N && !found; col++)
if (board[row][col] == 0) {
found = 1;
break;
}

if (!found) return 1;

row--; col--;
for (int num = 1; num <= 9; num++) {
if (isValid(row, col, num)) {
board[row][col] = num;
if (solveSudoku())
return 1;
board[row][col] = 0;
}
}
return 0;
}

void printBoard() {
for (int i = 0; i < N; i++) {
if (i % 3 == 0 && i != 0) printf("---------------------
");
for (int j = 0; j < N; j++) {
if (j % 3 == 0 && j != 0) printf("| ");
printf("%d ", board[i][j]);
}
printf("
");
}
}

int main() {
srand(time(0));
// Add code to generate puzzle...
// Dummy board shown here
board[0][0] = 5; board[0][1] = 3; board[0][4] = 7;
// Fill rest with 0s
printBoard();
if (solveSudoku()) {
printf("Solved Sudoku:\n");
printBoard();
} else {
printf("No solution exists.");
}
return 0;
}
Sample Output
Below is a mock-up output of the Sudoku solver after execution:

Initial Puzzle:
530|070|00 0
600|195|00 0
098|000|06 0
------+-------+------
800|060|00 3
400|803|00 1
700|020|00 6
------+-------+------
060|000|28 0
000|419|00 5
000|080|07 9

Solved Puzzle:
534|678|91 2
672|195|34 8
198|342|56 7
------+-------+------
859|761|42 3
426|853|79 1
713|924|85 6
------+-------+------
961|537|28 4
287|419|63 5
345|286|17 9

You might also like