Recursion is not ending after 1 iteration https://online.codingblocks.com/app/player/212761/content/212856/4927/code-challenge
hi @asta
Refer this -->
//Rat In a Maze
#include<bits/stdc++.h>
using namespace std;\
bool rat_in_a_Maze(char input[1001][1001], int solution[1001][1001], int i, int j, int m, int n)
{
if(i==m-1 && j==n-1)
{
solution[i][j]=1;
//Print the path
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cout<<solution[i][j]<<' ';
}
cout<<endl;
}
cout<<endl;
return true;
}
if(i>m-1 || j>n-1)
{
return false;
}
if(input[i][j]=='X')
{
return false;
}
//Assume solution exists through current cell
solution[i][j]=1;
bool rightSuccess=rat_in_a_Maze(input, solution, i, j+1, m, n);
bool downSuccess=false;
if(rightSuccess==false)
{
downSuccess=rat_in_a_Maze(input, solution, i+1, j, m, n);
}
//backtracking
solution[i][j]=0;
if(rightSuccess||downSuccess)
{
return true;
}
return false;
}
int main()
{
int m,n;
cin>>m>>n;
char input[1001][1001];
int solution[1001][1001];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cin>>input[i][j];
}
}
bool ans=rat_in_a_Maze(input, solution, 0,0,m,n);
if(ans==false)
{
cout<<-1<<endl;
}
}
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.