Rat in a maze problem: My code (as illustrated by sir) is printing the correct path but it's also showing "No Path" which shouldn't happen.Please point out the mistake

#include<bits/stdc++.h>
#define ll long long
using namespace std;
char A[10][10];
int sol[10][10];
int m,n;
bool sayYorN(int i, int j){
if(i==m-1 && j==n-1){
sol[i][j]=1;
//print cout
for(int i=0;i<m;i++) { for(int j=0;j<n;j++) cout<<sol[i][j]<<" “; cout<<”\n"; } cout<<"\n";
return true;
}

sol[i][j]=1;
if(i<m-1 && A[i+1][j]!='X')    bool z=sayYorN(i+1,j);
if(j<n-1 && A[i][j+1]!='X')    bool z=sayYorN(i,j+1);
sol[i][j]=0;
return false;

}

int main(){
cin>>m>>n;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>A[i][j];
int x=sayYorN(0,0);
if(x==0) cout<<“no path”;
}

hello @Rhinorox
image

here u need to check value of z . after each function call.
and if z values comes out true then return true
.

What you are suggesting is for the condition when i have to just “predict whether the girl will reach to the end or not” but
here i have to predict as well as print the path.
So i added the modifications told by you and it’s not working.
So kindly see into it.

share ur code using cb ide


checck ur updated code.
increased both array sizes ( check constraint for the same)

added if satement that i expalined above

in case no path found u need to print -1 (refer output format)

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.