Group 8 Sudoku Project-2
Group 8 Sudoku Project-2
Data Structures
Part of the degree of
Submitted to
Dr. Hemraj Saini
Assistant Professor
Submitted by:
Name-Mehul Sharma Roll no /SAP ID - 1000024979
Name-Adarsh Tripathi Roll no /SAP ID - 1000025774
Name-Sanya Srivastava Roll no /SAP ID - 1000024814
Name-Anukriti Singh Roll no /SAP ID - 1000025966
Section - A Group No.- 8
SCHOOL OF COMPUTING
DIT UNIVERSITY, DEHRADUN
(State Private University through State Legislature Act No. 10 of 2013 of Uttarakhand and approved by UGC)
I hereby certify that the work, which is being presented in the Report, entitled “Write the
algorithm and program for the Sudoku puzzle. Inputs: Given a partially filled 9×9 2D array ‘grid
[9][9]’, the goal is to assign digits (from 1 to 9) to the empty cells so that every row, column,
and subgrid of size 3×3 contains exactly one instance of the digits from 1 to 9”, in partial
fulfilment of the requirement as part of the course Data Structures of the Degree of Bachelor
of Computer Science and Engineering and submitted to the DIT University is an authentic
record of my work carried out during the period 06/01/2025 to 20/05/2025 under the guidance
of Dr.Hemraj Saini.
Introduction 1
Project Description 2
Result analysis 7
Conclusion 8
Bibliography 9
Introduction
Solving a Sudoku puzzle manually requires analytical thinking and patience. However,
automating the solution presents a well-known challenge in computer science — constraint
satisfaction problems. A constraint satisfaction problem (CSP) is a problem where the solution
must satisfy a number of constraints or conditions. Sudoku fits perfectly into this domain, as
each number placed in the grid must adhere to strict placement rules.
For instance, if a digit "5" is already present in the second row and the second column, placing
another "5" in the same row or column is invalid. Likewise, placing the same digit within the
same 3×3 box violates the puzzle's constraints. These logical conditions make Sudoku an ideal
problem to be solved using backtracking — a common recursive technique in programming.
This project focuses on creating a C program that reads a partially filled Sudoku grid and
attempts to solve it using backtracking, providing a fully solved grid as the output, or indicating
if no valid solution exists.
Problem Statement:
The objective of this project is to develop a software application capable of solving a standard
9×9 Sudoku puzzle. The system should take a partially filled Sudoku grid as input and fill in the
missing values such that each row, each column, and each 3×3 subgrid contains all digits from 1
to 9 exactly once. The solver must use a systematic algorithm (such as backtracking) to ensure
accuracy and completeness of the solution. The application should be designed to handle valid
puzzles and output the solved grid efficiently.
System Requirements:
Hardware Requirements:
Processor:Intel i3 or higher
RAM: 4 GB minimum
Hard Disk: 100 MB of free disk space
Display: Standard display with minimum 1024×768 resolution
Input Devices: Keyboard (for optional manual input)
Software Requirements:
2. Validation Module
Row check
Column check
3×3 Subgrid check
4. Output Module
Pseudocode
FUNCTION readInput(grid)
FOR i = 0 to 8
FOR j = 0 to 8
READ grid[i][j]
END FOR
END FOR
RETURN grid
END FUNCTION
Flowchart Description
Start
Loop through rows (i = 0 to 8)
o Loop through columns (j = 0 to 8)
Input value into grid[i][j]
End
2. Validation Module
Pseudocode
FUNCTION isSafe(grid, row, col, num)
FOR i = 0 to 8
IF grid[row][i] == num OR grid[i][col] == num
RETURN False
END FOR
FOR i = 0 to 2
FOR j = 0 to 2
IF grid[startRow + i][startCol + j] == num
RETURN False
RETURN True
END FUNCTION
Flowchart Description
Start
Pseudocode
FUNCTION solveSudoku(grid)
FOR row = 0 to 8
FOR col = 0 to 8
IF grid[row][col] == 0
FOR num = 1 to 9
IF isSafe(grid, row, col, num)
grid[row][col] = num
IF solveSudoku(grid) == True
RETURN True
grid[row][col] = 0 // Backtrack
RETURN False
RETURN True
END FUNCTION
Flowchart Description
Start
Find first empty cell
Loop num from 1 to 9
o Check if isSafe()
If true → Place number and recurse
If recursion fails → Backtrack
If no number works → Return False
If all cells filled → Return True
End
4. Output Module
Pseudocode
FUNCTION printGrid(grid)
FOR i = 0 to 8
FOR j = 0 to 8
PRINT grid[i][j]
PRINT newline
END FUNCTION
Start
Loop through each row and column
Print each element
Print newline after each row
End
Pseudocode
FUNCTION checkGrid(grid)
IF any duplicate in row/column/box
PRINT "Invalid Puzzle"
RETURN False
RETURN True
END FUNCTION
Flowchart Description
Start
Scan rows, columns, subgrids
If duplicate found → Show error
Else → Proceed to solve
End
Description:
A 9×9 two-dimensional array is the primary data structure used to represent the Sudoku grid.
Each element in the array corresponds to a single cell in the Sudoku board. The array is typically
defined as int grid[9][9] in C++ or similar in other programming languages.
Purpose:
Example:
int grid[9][9] = {
{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
...
};
Advantages:
Description:
To improve the performance of the backtracking algorithm, three auxiliary boolean arrays are
sometimes used to track whether a specific number already exists in a row, column, or 3×3
subgrid.
Types:
Advantages:
Code
#include <stdio.h>
2. Validation Module
Code
int isSafe(int grid[9][9], int row, int col, int num) {
// Check row and column
for (int i = 0; i < 9; i++) {
if (grid[row][i] == num || grid[i][col] == num)
return 0;
}
return 1;
}
Code
int solveSudoku(int grid[9][9]) {
int row, col;
int emptyFound = 0;
if (!emptyFound)
return 1; // Puzzle Solved
4. Output Module
Code
void printGrid(int grid[9][9]) {
printf("\nSolved Sudoku Puzzle:\n");
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
printf("%d ", grid[i][j]);
}
printf("\n");
}
}
Code
int isValidInitialGrid(int grid[9][9]) {
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
int num = grid[row][col];
if (num != 0) {
grid[row][col] = 0;
if (!isSafe(grid, row, col, num)) {
return 0;
}
grid[row][col] = num;
}
}
}
return 1;
}
Code
int main() {
int grid[9][9];
readInput(grid);
if (!isValidInitialGrid(grid)) {
printf("Invalid initial puzzle configuration.\n");
return 0;
}
if (solveSudoku(grid)) {
printGrid(grid);
} else {
printf("No solution exists for the given Sudoku puzzle.\n");
}
return 0;
}
#include <stdio.h>
return 1;
}
if (!emptyFound)
return 1; // Puzzle solved
return 0;
}
readInput(grid);
if (!isValidInitialGrid(grid)) {
printf("Invalid initial puzzle configuration.\n");
return 0;
}
if (solveSudoku(grid)) {
printGrid(grid);
} else {
printf("No solution exists for the given Sudoku puzzle.\n");
}
return 0;
}
Input
530070000
600195000
098000060
800060003
400803001
700020006
060000280
000419005
000080079
Output
The Sudoku solver was tested with multiple input cases including standard, medium, and hard
puzzles. For all valid puzzles, the program correctly filled the empty cells while satisfying all
Sudoku constraints — no repetition in rows, columns, or 3×3 subgrids. If the input was
unsolvable or incorrect, the program promptly displayed a message indicating that no solution
exists. This confirms the reliability of the implemented logic.
Performance Evaluation
The algorithm follows a backtracking technique, which explores all possibilities in a structured
manner and retracts invalid paths efficiently. For puzzles with a unique solution and sufficient
clues, the program executed within a fraction of a second. Even in cases with sparse clues, the
solver completed execution without noticeable delay on a standard personal computer. Thus, the
program demonstrates efficient runtime performance for real-world applications.
This confirms that the output is correct and fully adheres to the rules of the game.
Observed Limitations
The program does not handle puzzles with multiple solutions; it returns the first one it
finds.
It expects all values between 0–9 as input. No advanced input validation is included for
out-of-range or non-numeric entries.
The user interface is console-based; there is no graphical interface or puzzle generation
functionality.
Outcome
The program consistently returns correct solutions for solvable puzzles and clearly indicates
when no solution exists. It ensures that:
Each digit from 1 to 9 appears exactly once in each row, column, and 3×3 subgrid.
The input is processed correctly using a simple command-line interface.
Users receive either a fully solved grid or a message explaining that the puzzle has no
solution.
The solver has been tested against multiple test cases and produced correct outputs, proving the
algorithm’s robustness and correctness. The solution demonstrates the effective use of recursion
and logic-based backtracking.
Final Remark
This project demonstrates how classical algorithms like backtracking can be applied to real-
world problems. The success of this implementation shows the power of structured problem-
solving techniques and lays the groundwork for further development, such as adding a graphical
interface, puzzle difficulty grading, or automated puzzle generation.