Raise a doubt for Sudoku Solver problem 5 days ago and also tried to follow up with some TA’s but hard luck.
Please look into this and close this at priority.
Raise a doubt for Sudoku Solver problem 5 days ago and also tried to follow up with some TA’s but hard luck.
Please look into this and close this at priority.
yes, this is the one.
You are not referring updated code. Check below code for your reference and please go through the entire comment section where I raised my doubt specifically.
@PThak2018 you dont have to have a pair for each configuration and whether its solvable or not, you can simply have the return type of the “solve” function as bool, and you can check from there if a particular configuration is solvable or not. We pass the sudoku by reference so any changes made anywhere in the program are reflected in the sudoku.
I am attaching a function here and explaining it below as well, please see
base case: if n reaches i, it means there are no more rows to be traversed and the sudoku is solved. at this point, print the sudoku. if j >= 9, then we can simply make a recursive call for the next row and start from the 0th column. If a column if already filled, you dont have to do anything, so simply call for next column. To solve, we try putting 1-9 numbers in it, and call for rest of the sudoku.
bool sudokuSolver(int sudoku[9][9], int n, int i = 0, int j = 0){
//base case
if(i == n){
//print sudoku
for(int x = 0; x < n; x++){
for(int y = 0; y < n; y++){
cout << sudoku[x][y] << " ";
}
cout << endl;
}
return true;
}
//row change
if(j == n){
return sudokuSolver(sudoku, n, i + 1, 0);
}
//non-empty
if(sudoku[i][j] != 0){
return sudokuSolver(sudoku, n, i, j + 1);
}
//solve
for(int number = 1; number <= n; number++){
if(isValid(sudoku, n, i, j, number)){
sudoku[i][j] = number;
bool isSolvable = sudokuSolver(sudoku, n, i, j + 1);
if(isSolvable){
return true;
}
}
}
sudoku[i][j] = 0;
return false;
}
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.