Rat in a maze (Not able to pass 4th test case)

#include
using namespace std;

bool rat(char inp[][1000],int out[][1000],int n,int m,int i,int j){
if(i==(n-1)&&j==(m-1)) {
out[i][j]=1;
for(int row=0;row<n;row++){
for(int col=0;col<m;col++) cout<<out[row][col]<<’ ‘;
cout<<’\n’;
}
return true;
}

if(inp[i][j]=='X') return false;

if(i>n||j>m) return false;

out[i][j]=1;

if(rat(inp,out,n,m,i,j+1))
    return true;    

if(rat(inp,out,n,m,i+1,j))
    return true;

out[i][j]=0;
return false;

}

int main() {
int n,m;
char inp[1000][1000];
int out[1000][1000]={0};
cin>>n>>m;
for(int i=0;i<n;i++)
cin>>inp[i];
rat(inp,out,n,m,0,0);
return 0;
}

Print -1 for the 4th test case (No path found):wink:

1 Like