Not able to think of approach


how can we make the rat move in left and up direction?

Hey @prerna_gupta31, we have to make rat move only in two directions I.e., right and downward direction of the given matrix or else you won’t be able to get at the end point I.e., rat won’t be able to hit cheese.
In order to move right & downward direction , suppose you are at (I,j) position so right will be (I,j+1) & downward direction will be (I+1, j) do this until you hit the cheese. Hope this will help you :grinning:

https://ide.codingblocks.com/s/342952 In that case this code should work… right? but 2 testcases are failing

Code is implemented in a very good approach but you forgot to backtrack in your this loop if(i==m-1 and j==n-1)


Make sol[i][j]=0 after line 23 too, so that you can backtrack to. Hope this would have helped :grinning:

I made the changes still it does not work for 2 test cases

Tell me the test case numbers so that I can check what issue you are facing.

Hey @prerna_gupta31 try to implement this in your code


It will make your failed test cases pass .
I guess you’ll be needing my code for reference, do let me know if you want to. And send me your backtracked code to which i suggested you to do.

Do let me know if it works or not. If it doesn’t I will provide you with my own code :grinning:

hey!.. 2 testcases are still failing

@prerna_gupta31 please send me your finalised code and test case number to . I’ll send you my personal code :grinning:

https://ide.codingblocks.com/s/342993 This is the finalized code… testcase where it is failing is unknown…

Okay @prerna_gupta31, i will debug it and send you the proper code. Just have patience till then :smile:

Ohhho @prerna_gupta31 your entire code is absolutely correct. Why is it not passing two test cases is because of your print statement in if condition of main which is:

if(!ratinmaze(maze,m,n,0,0,sol))
cout<<“NO PATH FOUND”;

instead of NO PATH FOUND just print “-1” without semi colons . This will get accepted as i have debugged it many time :smile:

that was rat in a maze problem… in this problem we will have to print “no path found” this is rat chases cheeze problem

Hey @prerna_gupta31 , there was a technical glitch due to which It was showing me some other problem. Back to your issue, I have read this question and also have debugged it. It’s missing side cases for eg:
4 5
OOXXX
OXOOO
OXOXO
OOOXO
for this it is showing no path found whereas it has one. Try to do for this Side cases. It will be fine then :grinning: