Rat chases its cheese

it shows segmentation fault

#include
using namespace std;
bool RatinMaze(char maze[][1001],int soln[][1001],int m,int n,int i,int j){
// base case
if(i==m && j==n){
soln[m][n]=1;
// prints
for(int i=0;i<=m;i++){
for(int j=0;j<=n;j++){
cout<<soln[i][j]<<" ";
}cout<<endl;
}
return true;
}
// recursive case
if(i<0 || j<0){
return false;
}
if(i>m or j>n ){
return false;
}
if(maze[i][j]==‘X’){
return false;
}
soln[i][j]=1;
bool right=RatinMaze(maze,soln,m,n,i,j+1);
if(right)
return true;
bool down=RatinMaze(maze,soln,m,n,i+1,j);
if(down){
return true;

}
bool up=RatinMaze(maze,soln,m,n,i-1,j);
if(up){
	return true;

}
bool left=RatinMaze(maze,soln,m,n,i,j-1);
if(left){
	return true;

}
soln[i][j]=0;

return false;

}
int main() {
int m,n;cin>>m>>n;
char maze[1001][1001];int soln[1001][1001];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cin>>maze[i][j];
}
}
bool ans=RatinMaze(maze,soln,m-1,n-1,0,0);
if(!ans){
cout<<"-1";
}
}

hi @kumarakash121005
try this -->

please tell me the logic for this and flow of the code

hi @kumarakash121005
this is a backtracking based problem… maybe u can go through the n queen problem, sir has explained that in depth and all backtracking problems have similar logic…
rest try to dry run the code, u will get better insight…

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.