wHY MY CODE IS INCORRECT
#include
using namespace std;
bool rat(char maze[][10],int sol[][10],int i,int j,int n,int m)
{
if(i==n && j==m)
{
sol[n][m]=1;
for(int i=0;i<=n;i++) //change
{
for(int j=0;j<=m;j++) //change
{
cout<<sol[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
return true;
}
if(i>n || j>m)
return false;
if(maze[i][j]=='X') //change
return false;
sol[i][j]=1;
if(j+1<=m && maze[i][j+1]!='X')
{
bool right=rat(maze,sol,i,j+1,n,m);
if(right)
{
return true;
}
}
if(i+1<=n && maze[i+1][j]!='X')
{
bool down=rat(maze,sol,i+1,j,n,m);
if(down)
{
return true;
}
}
if(i-1>=n && maze[i-1][j]!='X')
{
bool up=rat(maze,sol,i-1,j,n,m);
if(up)
{
return true;
}
}
if(j-1>=m && maze[i][j-1]!='X')
{
bool left=rat(maze,sol,i,j-1,n,m);
if(left)
{
return true;
}
}
sol[i][j]=0;
/*
if(right || down){
return true;
}
*/
return false;
}
int main() {
int n,m;
cin>>n>>m;
char maze[10][10];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>maze[i][j];
}
}
int sol[10][10]={0};
bool ans= rat(maze,sol,0,0,n-1,m-1);
if(ans==false)
cout<<“NO PATH FOUND”<<endl;
return 0;
}