I am not able to print 1 path and can you tell how to pass 2d array call by reference

#include
using namespace std;

bool ratMaze(char a[1000][1000],int sol[][1000], int i,int j,int m,int n)
{
if(i==m&&j==n)
{
sol[i][j]=1;
for(int i=0;i<=m;i++)
{
for(int j=0;j<=n;j++)
{
cout<<sol[i][j]<<" ";
}
cout<<endl;
}
return true;
}
if(i>m || j>n)
{
return false;
}

if(a[i][j]=='X')
{
    return false;
}

sol[i][j]=1;

bool right= ratMaze(a,sol,i,j+1,m,n);
bool down= ratMaze(a,sol,i+1,j,m,n);

sol[i][j]=0;

if(right|| down)
{
    return true;
}
return false;

}

int main()
{

int m,n;
cin>>m>>n;
char a[1000][1000];

int sol[1000][1000]={0};

for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cin>>a[i][j];
}
}

int i=0,j=0;

bool ans=ratMaze(a,sol,i,j,m-1,n-1);

if(ans==false)
{
cout<<-1;
}
// else{
// for(int i=0;i<=m;i++)
// {
// for(int j=0;j<=n;j++)
// {
// cout<<sol[i][j]<<" ";
// }
// cout<<endl;
// }
// }

return 0;
}

to print only one path (rightmost path)
check after calling right whether right path exist or not
if exists then return true no need to call down

check modified code below
Modified Code


now this code is passing all testcases

and By default array are passed by reference
if you want to print solution maze in main you can also do that
just move line no 36 after return true statement so that changes will not undo if solution exists
changes will undo only when path doesn’t exists
check the above code and try to print solution in main also I have already done this

i hope now your doubt is resolved
if yes give your feedback from below link
else feel free to ask

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.

ok i understood the solution, my one more question is how to pass 2d array call by reference

hi @anandsingh3210
U have reopened the doubt… is there still anything??

yes i want to know how to pass 2d array call by reference suppose i want to print solution in the main function

We are using recursion over here, hence final ans will be printed in the base case only… else u will have to unnecessarily pass an additional array to store the ans…

I know but just show me how do we pass 2d array call by reference

hi @anandsingh3210
I have answered ur query in chat… kindly check it…

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.