I am not able to get why did we return false in base case to print all possible solutions
How will the function execute if we dry run it??
Doubt regarding printing multiple solutions
Hello @sanjana23,
-
When it is required to print any one of the possible solutions:
We return true in the base case.
This will indicate that we have found one of the possible way, so we need not to process any further.
But, how and where is this happening in our code?
If you see lines 57 to 60 at 19:34,
There is a check,
a. if the returned value from the next recursion call is true, then we would simply return true from the current function definition.
b. else we will keep on processing -
Now, consider your case, where you have to print all possible arrangements of queens:
you would require your code to go for option b.
This way it will backtrack and would look for another solution.
Thus, returning false will indicate that you need not to stop processing. There might be few more possible way.
So, check for those also.
Then when would the entire processing terminate? would it run infinitely?
Answer is No.
After reaching base cases, it will return back to previous step and would look for another possible way from there.
If it exist and it would go for that option.
else, it will return back to the function in which it was called.
Similar, procedure would be followed for all function call.
This way it would finally terminate.
Example:
Consider it as a tree:
The leaf nodes are the base conditions
And you have traverse all the possible paths in the tree.
Hope, this would help.
Give a like, if you are satisfied.