SudoSolver Problem


where I am making mistake

for(int num=1; num <=n; num++) {
if(mat[i][j] == 0) {
if(canPlace(mat,i,j,n,num)) {
//cout << “Ture”;
// Assume that if the correct ones
mat[i][j] = num;
// check another subproblem
bool couldSuccess = sudoSolver(mat, i+1, j , n); // check on the next cell
for next cell you have to increase j not i
so correct one is:bool couldSuccess = sudoSolver(mat, i, j+1 , n); //
if(couldSuccess == true) {
return true;
}
}
}
you have to reset value here means put 0 here before moving to next num
mat[i][j]=0;
}
// recursion point fails
mat[i][j] = 0;
no need for this now
return false;
}

If the canplace return false then our value is alread set to zero why we need to do its again ?

we are not only come at that point when canplace() is returning false
we are also come here when recursion return false and then we try next num so before checking for next no we have undo our change by placing 0

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.