Sudoku Project Report Complete
Sudoku Project Report Complete
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 9
int board[N][N];
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