This code showing error

https://ide.codingblocks.com/s/646797 m was not declared in this scope

hey @mohit26900
you have declared m inside main function so the scope of m is limited to main .
you have also passed m as a parameter to the rat_in_maze()function but you cannot set m as the array size as it is just passed into the function.
there are two ways to solve this issue
1)m and n must be declared globally if you want to use m as a size of array.
2)predefined the size of array as maximum possible array size in this case it is 1000.

refer below code for more clarity
#include

using namespace std;

bool rat_in_maze(char matrix[1000][1000],int sol[1000][1000],int i,int j,int m,int n)

{

if(i==m && j==n)

{

    sol[i][j]=1;

    for(int ii;ii<=m;ii++)

    {

        for(int jj=0;jj<=n;jj++)

        {

            cout<<sol[ii][jj]<<" ";

        }

        cout<<endl;

    }

    cout<<endl;

    return true;

}

if(i>m || j>n)

{

    return false;

}

if(matrix[i][j]=='#')

{

    return false;

}

sol[i][j]=1;

bool rightsuccess=rat_in_maze(matrix,sol,i,j+1,m,n);

bool downsuccess=rat_in_maze(matrix,sol,i+1,j,m,n);

sol[i][j]=0;

if(rightsuccess || downsuccess)

{

    return true;

}

return false;

}

int main() {

int m,n;

cin>>m>>n;

char matrix[1000][1000];

for(int i=0;i<m;i++)

{

    for(int j=0;j<n;j++)

    {

        cin>>matrix[i][j];

    }

}

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

int ans=rat_in_maze(matrix,sol,0,0,m-1,n-1);  

if(ans==false)

{

    cout<<"No Path Found";

}              

}

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.

#include using namespace std; bool rat_in_maze(char matrix[1000][1000],int sol[1000][1000],int i,int j,int m,int n) { if(i==m && j==n) { sol[i][j]=1; for(int ii;ii<=m;ii++) { for(int jj=0;jj<=n;jj++) { cout<<sol[ii][jj]<<" "; } cout<<endl; } cout<<endl; return true; } if(i>m || j>n) { return false; } if(matrix[i][j]==‘X’) { return false; } sol[i][j]=1; bool rightsuccess=rat_in_maze(matrix,sol,i,j+1,m,n); bool downsuccess=rat_in_maze(matrix,sol,i+1,j,m,n); sol[i][j]=0; if(rightsuccess || downsuccess) { return true; } return false; } int main() { int m1,n1; cin>>m1>>n1; char matrix[1000][1000]; for(int i=0;i<m1;i++) { for(int j=0;j<n1;j++) { cin>>matrix[i][j]; } } int sol[1000][1000] = {0}; int ans=rat_in_maze(matrix,sol,0,0,m1-1,n1-1); if(ans==false) { cout<<“No Path Found”; } }

https://ide.codingblocks.com/s/646797 showing TLE and error in 1 test case

hi @mohit26900
try this -->

//Rat In a Maze
#include<bits/stdc++.h>
using namespace std;\
bool rat_in_a_Maze(char input[1001][1001], int solution[1001][1001], int i, int j, int  m, int n)
{
	if(i==m-1 && j==n-1)
	{
		solution[i][j]=1;
		//Print the path
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				cout<<solution[i][j]<<' ';
			}
			cout<<endl;
		}
		cout<<endl;
		return true;
	}
	if(i>m-1 || j>n-1)
	{
		return false;
	}
	if(input[i][j]=='X')
	{
		return false;
	}
	//Assume solution exists through current cell
	solution[i][j]=1;
	bool rightSuccess=rat_in_a_Maze(input, solution, i, j+1, m, n);
	if(rightSuccess==false)
	{
	    bool downSuccess=rat_in_a_Maze(input, solution, i+1, j, m, n);
        if(!downSuccess){
            solution[i][j] = 0;
        }
        return downSuccess;
	}
    else{
        return true;
    }
    //backtracking
    return false;
}

int main()
{
   int m,n;
   cin>>m>>n;
   char input[1001][1001];
   int solution[1001][1001];
   for(int i=0;i<m;i++)
   {
   	for(int j=0;j<n;j++)
   	{
   		cin>>input[i][j];
   	}
   }
   bool ans=rat_in_a_Maze(input, solution, 0,0,m,n);
   if(ans==false)
   {
	   cout<<-1<<endl;
   }
}

whats wrong in my code it showing tle in one case only

hi @mohit26900
in ques its mentioned Find the rightmost path through which, rat can reach this position.
but u are not implementing that part… refer my code for the same… also if ans is not found u have to print -1 and not No Path Found

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.