Recursion and backtracking

Q1 if our rest of grid (from i+1) return false all the way up to from where it was called, then how is the for loop getting incremented and the next number is being placed.

Q2 after we tried all number in a box we come out of the loop , bactrack and return false. how is this false incrementing the for loop to place the next number in the previous box?

In both questions i want to understand how control transfers from return false to the for loop

hello @pranaynigam

here in line 71 , we placed a number to cell (i,j) and then made a call on (i,j+1) to solve remaining sudoku.

now here we will either receive true or false.
in case of true (sudoku is solved ) we are simply returning true line 76

otherwise loop will try next number at (i,j) and again repeat same steps.

understood.
now please explain the backtracking part of
mat[i][j]=0;
return false;
why are we doing it outside the loop, and how is the return false statement working trough the recursion tree?

see when we exhausted all paths from (i,j) but still not able to solve then in that case we need to reset mat[i][j] value to 0 and then we need to return false to its calling function as an indicator that sudoku is not solvable with current configuration.

now rewatch the video again it should be clear now

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.