the sir provides the code in the lecture i understand the code almost but there are few doubts for the give input which sir take in the lecture that is
0000
000
000
0*00
for this matrix maze we have find all possible paths
now intially i=0,j=0,m=3,n=3
maze[0][0] is not blocked therefore it set two
one
now we have two recursive call rightsuccess and downsuccess yahi se doubt aaya mujhe sir ki dono recursive calls saath saath execute hoogi ya phir phele khaali right vaali ya phir left valli khalli this is doubt would pls provide recursion tree for this problem just for one case or dry run for one case the code
Rat_in_a_maze doubt in dry run this code
@dipeshgupta197 both calls cannot be executed at the same time. Compiler can only execute one command at a time. Draw a call stack you’ll understand.
thats mean first it will go towards right recursively
i have my code print in last that path not exsist why it should print as finally bool ans =true
#include
using namespace std;
bool rat_in_maze(char maze[10][10],int sol[][10],int i,int j,int m,int n)
{
if(i==m && j==n)
{
sol[m][n]=1;
for(int i=0;i<=m;i++)
{
for(int j=0;j<=n;j++)
{
cout<<sol[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
}
if(i>m || j>n)
{
return false;
}
if(maze[i][j]=='*')
{
return false;
}
sol[i][j]=1;
bool rightsuccess=rat_in_maze(maze,sol,i,j+1,m,n);
bool downsuccess=rat_in_maze(maze,sol,i+1,j,m,n);
// backtracking
sol[i][j]=0;
if(rightsuccess || downsuccess)
{
return true;
}
return false;
}
int main()
{
char maze[10][10]={
“0000”,
“000",
"000”,
“0*00”,
};
int sol[10][10]={0};
int m=4;
int n=4;
bool ans=rat_in_maze(maze,sol,0,0,m-1,n-1);
if(ans==false)
{
cout<<“path doesnt exist”;
}
return 0;
}
https://drive.google.com/file/d/1yn21zOm6ZJKfd1yzLqbbKTEk1kkF9ZJe/view?usp=sharing
https://drive.google.com/file/d/1tLghr3wy14y9MoihyImuov3lIk1LkYnD/view?usp=sharing
THESE ARE THE DRY RUNS I DO AND UNDERSTAND
AS THE FINAL CALL 0 0 3 3 WILL RECEIVE TRUE FROM BOTH THE RIGTH SUCCESS AS WELL DOWN SUCCESS
SO SOL[0][0]=0;
IF CONDITION TRUE
THEN RETURN TRUE TO MAIN
THEN WHY WE PRINT PATH DOESNOT EXIST