#include
using namespace std;
bool ratinmaze(char maze[1000][1000],int soln[][1000],int i,int j,int m,int n)
{
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;
}
if(i>m || j>n)
{
return false;
}
if(maze[i][j]==‘X’)
{
return false;
}
soln[i][j]=1;
bool right=ratinmaze(maze,soln,i,j+1,m,n);
bool down=ratinmaze(maze,soln,i+1,j,m,n);
soln[i][j]=0;
if(right||down)
{
return true;
}
return false;
}
int main() {
int n,m;
cin>>m>>n;
char maze[1000][1000];
int soln[1000][1000]={0};
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cin>>maze[i][j];
}
}
bool ans=ratinmaze(maze,soln,0,0,m-1,n-1);
if(ans==false)
{
cout<<”-1";
}
return 0;
}
What is wrong with the code
You are not considering the fact that last cell could be empty.Consider the case where last cell/destination cell is blocked.
*You are not considering the fact that last cell could be blocked.Consider the case where last cell/destination cell is blocked.
so i should i one more if condition if maze[m][n]=‘x’
Yes,because as such no other error i can found in your code and your logic is correct.
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.