I want to ask that in " Rat in a maze problem " program there are two return statements that I have mentioned in program and my doubt is that are they just for recursive purpose i.e. they are not doing anything other than returning to previous function.
Regarding Rat in a Maze problem
No, the calls done here
bool rightSuccess = ratInMaze(maze,soln,i,j+1,m,n);
bool downSuccess = ratInMaze(maze,soln,i+1,j,m,n);
are responsible for calling recursive functions, whereas this is your base case
if(i==m && j==n){
soln[m][n]=1;
for(int i=0;i<=m;i++){
for(int j=0;j<=n;j++){
cout<<soln[i][j]<<" ";
}cout<<endl;
}cout<<endl;
return true; // HERE
}
and this will check that ,whatever comes from right and down call if either of them is true you will return true
if(rightSuccess || downSuccess){ // HERE
return true;
}
So can I conclude that those return statements have nothing to do with printing of the paths it is just for recursion.
which statement, please specify
Those in which I have commented “HERE”
This is responsible for printing path
if(i==m && j==n){
soln[m][n]=1;
for(int i=0;i<=m;i++){
for(int j=0;j<=n;j++){
cout<<soln[i][j]<<" ";
}cout<<endl;
}cout<<endl;
return true; // HERE
}
another one is just to check the answer from right and down call.
base case’ return true and if(rightSuccess || downSuccess) -> return true / false , basically the lines where I have commented “HERE”.
bro i already mentioned that
This is responsible for printing path
if(i==m && j==n){
soln[m][n]=1;
for(int i=0;i<=m;i++){
for(int j=0;j<=n;j++){
cout<<soln[i][j]<<" ";
}cout<<endl;
}cout<<endl;
return true; // HERE
}
and this is to check that ,whatever comes from right and down call if either of them is true you will return true
if(rightSuccess || downSuccess){ // HERE
return true;
}
Now tell me is it clear? if not , then mention with detail what is not clear.
mera doubt ye hai ki if(rightSucess || downSuccess) ya to true dega ya false to true ya false se kya benefit hai kyuki we are just printing path to mai ye puchna chahta hu ki return true ya return false sirf pichli call me jane ke liye hi toh use hue hai
ha, isse hum recursive tree mai uppr jayenge return krte hue. Agar true return kiya iska mtlb yeh hai ki path exist krta hai agay ki calls mai(down of recursive tree) false return kiya toh path exist ni krta agay ki calls ke liye. Got it?
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.