sudoku solve Algorithm
The Sudoku Solver Algorithm is an advanced technique designed to solve Sudoku puzzles by implementing various logical strategies and computational methods. It aims to find the correct numerical sequence for each row, column, and 3x3 grid in the puzzle without violating the basic rules of Sudoku. The algorithm is capable of solving puzzles of varying difficulty levels, from simple to highly complex ones. The core idea behind this algorithm is to use a combination of logic-based techniques, such as naked singles, hidden singles, locked candidates, and more, along with backtracking, which is a form of trial-and-error, to deduce the correct values for each cell in the puzzle.
One of the most popular techniques used in the Sudoku Solver Algorithm is backtracking, which involves placing a number in a cell and then proceeding to the next cell to place another number. If the algorithm reaches a point where the current number cannot be placed without violating the puzzle's rules, it will backtrack to the previous cell and try a different number. This process continues until the solution is found or all possibilities are exhausted. Another essential aspect of the algorithm is the elimination method, which systematically narrows down the possible values for each cell by examining the numbers present in the corresponding row, column, and 3x3 grid. By combining these techniques, the Sudoku Solver Algorithm can efficiently solve even the most challenging puzzles, making it a valuable tool for both casual players and competitive Sudoku enthusiasts.
#include <iostream>
using namespace std;
///N=9;
int n = 9;
bool isPossible(int mat[][9], int i, int j, int no)
{
///Row or col nahin hona chahiye
for (int x = 0; x < n; x++)
{
if (mat[x][j] == no || mat[i][x] == no)
{
return false;
}
}
/// Subgrid mein nahi hona chahiye
int sx = (i / 3) * 3;
int sy = (j / 3) * 3;
for (int x = sx; x < sx + 3; x++)
{
for (int y = sy; y < sy + 3; y++)
{
if (mat[x][y] == no)
{
return false;
}
}
}
return true;
}
void printMat(int mat[][9])
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << mat[i][j] << " ";
if ((j + 1) % 3 == 0)
{
cout << '\t';
}
}
if ((i + 1) % 3 == 0)
{
cout << endl;
}
cout << endl;
}
}
bool solveSudoku(int mat[][9], int i, int j)
{
///Base Case
if (i == 9)
{
///Solve kr chuke hain for 9 rows already
printMat(mat);
return true;
}
///Crossed the last Cell in the row
if (j == 9)
{
return solveSudoku(mat, i + 1, 0);
}
///Blue Cell - Skip
if (mat[i][j] != 0)
{
return solveSudoku(mat, i, j + 1);
}
///White Cell
///Try to place every possible no
for (int no = 1; no <= 9; no++)
{
if (isPossible(mat, i, j, no))
{
///Place the no - assuming solution aa jayega
mat[i][j] = no;
bool aageKiSolveHui = solveSudoku(mat, i, j + 1);
if (aageKiSolveHui)
{
return true;
}
///Nahin solve hui
///loop will place the next no.
}
}
///Sare no try kr liey, kisi se bhi solve nahi hui
mat[i][j] = 0;
return false;
}
int main()
{
int mat[9][9] =
{{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}};
printMat(mat);
cout << "Solution " << endl;
solveSudoku(mat, 0, 0);
return 0;
}