I am able to print all the solutions(possible paths) . Please guide me how can I print the one path only that is the right most path.
here is the code:-
#include
using namespace std;
bool ratInMaze(char maze[1000][1000],int sol[1000][1000],int i,int j,int m,int n){
// base case
if(i==m && j==n){
sol[m][n]=1;
// printing the sol array
for(int i=0;i<=m;i++){
for(int j=0;j<=n;j++){
cout<<sol[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
return true;
}
// rec case
/// rat out of the grid
if(i>m || j>>n){
return false;
}
// if obstacle comes
if(maze[i][j]=='X'){
return false;
}
sol[i][j]=1;
bool forwardSuccess=ratInMaze(maze,sol,i,j+1,m,n);
bool downSuccess=ratInMaze(maze,sol,i+1,j,m,n);
sol[i][j]=0; // backtracking
if(forwardSuccess || downSuccess){
return true;
}
return false;
}
int main() {
int m,n;
cin>>m>>n;
char maze[1000][1000];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cin>>maze[i][j];
}
}
int sol[1000][1000]={0};
bool ans=ratInMaze(maze,sol,0,0,m-1,n-1);
if(ans==false){
cout<<"-1";
}
}