0% found this document useful (0 votes)
39 views4 pages

Assignment 2 Code Output Vishesh

The document contains code for solving a Sudoku puzzle using backtracking. It defines functions for finding unassigned locations, checking if a number placement is valid in a row/column/box, recursively solving the puzzle, and printing the grid. The main function gets user input, calls the solving function, and prints the original and solved grids.
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)
39 views4 pages

Assignment 2 Code Output Vishesh

The document contains code for solving a Sudoku puzzle using backtracking. It defines functions for finding unassigned locations, checking if a number placement is valid in a row/column/box, recursively solving the puzzle, and printing the grid. The main function gets user input, calls the solving function, and prints the original and solved grids.
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/ 4

Name- Vishesh Yadav

Roll no - 205

Code -

#include <stdio.h>

#define UNASSIGNED 0

#define N 9

int FindUnassignedLocation(int grid[N][N], int *row, int *col);

int isSafe(int grid[N][N], int row, int col, int num);

int SolveSudoku(int grid[N][N]);

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] == UNASSIGNED)

return 1;

return 0;

int UsedInRow(int grid[N][N], int row, int num) {

for (int col = 0; col < N; col++)

if (grid[row][col] == num)

return 1;

return 0;

int UsedInCol(int grid[N][N], int col, int num) {

for (int row = 0; row < N; row++)

if (grid[row][col] == num)

return 1;
return 0;

int UsedInBox(int grid[N][N], int boxStartRow, int boxStartCol, int num) {

for (int row = 0; row < 3; row++)

for (int col = 0; col < 3; col++)

if (grid[row + boxStartRow][col + boxStartCol] == num)

return 1;

return 0;

int isSafe(int grid[N][N], int row, int col, int num) {

return !UsedInRow(grid, row, num) && !UsedInCol(grid, col, num) &&

!UsedInBox(grid, row - row % 3, col - col % 3, num) && grid[row][col] == UNASSIGNED;

int SolveSudoku(int grid[N][N]) {

int row, col;

if (!FindUnassignedLocation(grid, &row, &col))

return 1;

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

if (isSafe(grid, row, col, num)) {

grid[row][col] = num;

if (SolveSudoku(grid))

return 1;

grid[row][col] = UNASSIGNED;

}
return 0;

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");

int main() {

int grid[N][N];

printf("Enter the Sudoku grid (9x9):\n");

for (int i = 0; i < N; i++) {

for (int j = 0; j < N; j++) {

scanf("%d", &grid[i][j]);

printf("\nOriginal Sudoku grid:\n");

printGrid(grid);

if (SolveSudoku(grid) == 1) {

printf("\nSolution:\n");

printGrid(grid);

} else

printf("No solution exists");

return 0;

}
Ouput:

You might also like