-
Sudoku solver
Hello, I'm asked to make a program that will solved a 9 X 9 sudoku using backtracking.
I am having a problem in backtracking. It backtracks but it do not know if it already tested a number so it creates an infinite loop. I do not know what to do.
Please help me.
Code:
//===============================================================================//
void backtrack(int a, int b, int value)
{
bool check = true;
if(sudoku2[a][b] == 0 ) // This will check the contents of the array "sudoku2" and change the ones with the value of zero
{
check = true;
while(check == true && value < 9)
{
value++;
sudoku2[a][b] = value; // assigns a value to sudoku2[a][b]
if((check_row(a, b)) && (check_col(a, b)) && (check_box(a, b))) // if there is no conflict with the value. the previous while and if statements will terminate and moves to the next one.
{
cout << "lol3";
check = false;
}
else // If the value is not compatible, it will try the next value
{
if(value > 8)// If there is no value from 1 to 9 that is compatible, backtrack
{
sudoku2[a][b] = 0;
do
{
if(b > 0)
{
b = b - 1;
}
else
{
a = a - 1;
b = 0;
}
}while(sudoku[a][b] != 0);
value = sudoku2[a][b];
cout << value;
sudoku2[a][b] = 0;
backtrack(a, b, value);
}
}
}
}
if(b < 8)
{
backtrack(a, b + 1, 0);
}
else
{
backtrack(a + 1, 0, 0);
}
}
//===============================================================================//
-
Can you outline the algorithm you're trying to use? Also what are the starting conditions with which you call backtrack()?