Rat chases its cheese


why some testcases failing

Hello @Vibhuti0206,

There are few mistakes in your code.
Refer to the following code to see the difference:

Hope, this would help.
Give a like if you are satisfied.

https://ide.codingblocks.com/s/235189,made changes…only 1 test case failing why?

https://ide.codingblocks.com/s/235189,what is the error???

Hey @Vibhuti0206,

Both the links don’t contain any code.

code is opening at my side https://ide.codingblocks.com/s/235189 through this link

#include<bits/stdc++.h> using namespace std; bool ratInMaze(char maze[10][10],int soln[10][10],int i,int j,int n,int m){ if(i==n&&j==m && maze[i][j]!=‘X’){ soln[n][m]=1; for(int i=0;i<=n;i++){ for(int j=0;j<=m;j++){ cout<<soln[i][j]<<" "; } cout<<endl; } return true; } if(i>n||j>m||i<0||j<0){ return false; } if(maze[i][j]==‘X’){ return false; } bool right,down,up,left; soln[i][j] = 1; if(soln[i+1][j]==0){ down = ratInMaze(maze,soln,i+1,j,n,m); } if(soln[i][j+1]==0){ right = ratInMaze(maze,soln,i,j+1,n,m); } if(soln[i-1][j]==0){ up = ratInMaze(maze,soln,i-1,j,n,m); } if(soln[i][j-1]==0){ left = ratInMaze(maze,soln,i,j-1,n,m); } if(right||down||left||up){ return true; } soln[i][j] = 0; return false; } int main() { int n,m; cin>>n>>m; char maze[10][10]; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ cin>>maze[i][j]; } } int soln[10][10]={0}; bool ans = ratInMaze(maze,soln,0,0,n-1,m-1); if(ans==false){ cout<<“NO PATH FOUND”; } return 0; }

Hello @Vibhuti0206,

It is failing for the case of “no path found”
Solution:
Initialize the boolean variable by false.

Corrected Code:

Hope, this would help.
give a like if you are satisfied.

https://ide.codingblocks.com/s/235916,it is even passing all the testcases like this…here i have not used !down and similar contditions??? is it imp to use those conditions and what exactly are they used for?

why do we have to initialise bool down up right left to false??

Hello @Vibhuti0206,

The test cases in this problem might not be covering all the possible cases.

Why that is important?

As you require a single path.
There could many possible paths for rat to reach the destination.
In that case you end up having the wrong path i.e. which is not required.

So, the order of movement.

But, here both will give correct answer.
Reason:
When you’ll reach the final cell from one path, you’ll mark it as 1.
So, it will give false for all other paths.

What i have done in the code will save the save the number of recursive calls only.

i got that extra condiitions,but now why to initialise? is it like if we consider the case like: 3 3 OOX;OOX;XXX…here it will be stuck to (0,0) cell after taking a round at first 4 cells?? like since we have not initialised anything,so ultimately when we get to the condition if(down||up||right||left) any of the 4 wouldnot be neither true nor false,so the code would not proceed??? is it like that?

Hello @Vibhuti0206,

Only global variables are assigned 0 (false) by default, any local variables are given a non-zero garbage value, which would evaluate to true in a boolean variable.

Thus, i initialized it with false.

It was failing for case like:
5 7
OXOOOOX
OXOXOXX
OXOXOOX
OOOXOXX
XXOXXXX

Suggestion:
Use cout statements to see the execution.

i got that,thankkyou