I have attached a photo in which digits written in blue denotes the block which are already filled and I have tried to check how the program will run (the digits which are written with pink ink) isme mujhe ek confusion hai jab 1st row ke 2nd last element se next call hogi toh koi bhi number next block m nahi aa payega toh uss block ko 0 karke orr return false hoga lekin vo return false aise karte karte uss 1st block m pahuch jayenga jha first humne nikala tha ki konsa number aayega but I can’t understand how all previous elements will become 0 kyuki sabme return false hota rahega orr for loop se bahar toh niklenege nahi
Regarding Sudoku Solver program
The solution of your sudoku is -
5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
If we are not able to place any number at a particular cell, then we backtrack and check for the remaining numbers for previous cells. This process goes on till we find a solution.
I know that it will backtrack but I have different doubt regarding backtracking. Please go through my doubt once again.
Yes, I get your doubt. You just want to know how the code will work when there is no solution to a puzzle, right?
Actually my doubt is:-
My doubt is that in the photo that I have attached in the doubt the nos. which are written in pink denotes the number which can be filled in empty blocks of 1st line so when we come in last block of 1st row we can’t put any number so we will backtrack from last block by making last block as 0 and returning false to second last block and subsequently we will return false to all the calls we have made. But my question is that after returning to second last block (as false) we will just return subsequently and at that point of time we are in for loop so we have not reached till backtracking’s code so we have not made the blocks as 0. This is my question
After putting 9 in second last block, you move to the last block. But there is no number left for the last block which can give us a solution. So we backtrack to the second last block and check for remaining numbers. But all numbers are exhausted for second last block as the for loop starts from 1 and ends at 9. So we backtrack to the third last block, where we have 4. The code now checks for next numbers: 5,6,7,8,9 and tries to find a solution. If it doesn’t, it backtracks to the block where we have 2 and tries to put some other value there.
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.