0% found this document useful (0 votes)
132 views2 pages

Sudoku

This C++ program solves Sudoku puzzles using backtracking. It takes an unfinished 9x9 Sudoku grid as input. The SolveSudoku function recursively tries inserting numbers from 1-9 into empty spaces, checking for validity after each insertion using auxiliary functions. If a partial solution cannot be completed, it backtracks by removing the last number inserted. Once a fully solved grid is found, the grid is printed.

Uploaded by

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

Sudoku

This C++ program solves Sudoku puzzles using backtracking. It takes an unfinished 9x9 Sudoku grid as input. The SolveSudoku function recursively tries inserting numbers from 1-9 into empty spaces, checking for validity after each insertion using auxiliary functions. If a partial solution cannot be completed, it backtracks by removing the last number inserted. Once a fully solved grid is found, the grid is printed.

Uploaded by

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

#include <iostream>

#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
#define UNASSIGNED 0
#define N 9

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


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

/* assign values to all unassigned locations for Sudoku solution


*/
bool SolveSudoku(int grid[N][N])
{
int row, col;
if (!FindUnassignedLocation(grid, row, col))
return true;
for (int num = 1; num <= 9; num++)
{
if (isSafe(grid, row, col, num))
{
grid[row][col] = num;
if (SolveSudoku(grid))
return true;
grid[row][col] = UNASSIGNED;
}
}
return false;
}

/* Searches the grid to find an entry that is still unassigned. */


bool 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 true;
return false;
}

/* Returns whether any assigned entry n the specified row matches


the given number. */
bool UsedInRow(int grid[N][N], int row, int num)
{
for (int col = 0; col < N; col++)
if (grid[row][col] == num)
return true;
return false;
}

/* Returns whether any assigned entry in the specified column matches


the given number. */
bool UsedInCol(int grid[N][N], int col, int num)
{
for (int row = 0; row < N; row++)
if (grid[row][col] == num)
return true;
return false;
}

/* Returns whether any assigned entry within the specified 3x3 box matches
the given number. */
bool 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 true;
return false;
}

/* Returns whether it will be legal to assign num to the given row,col location.
*/
bool 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);
}

/* print grid */
void printGrid(int grid[N][N])
{
for (int row = 0; row < N; row++)
{
for (int col = 0; col < N; col++)
cout<<grid[row][col]<<" ";
cout<<endl;
}
}

/* Main */
int main()
{ int grid[9][9];
int x,y;
cout<<"Enter the sudoku problem:\n";
for(x=0;x<9;x++)
{
cout<<"Row "<<x+1<<" :";
for(y=0;y<9;y++)
{
cin>>grid[x][y];
}
}
if (SolveSudoku(grid) == true)
printGrid(grid);
else
cout<<"No solution exists"<<endl;
return 0;
}

You might also like