The first two testcases are giving wrong ans. Pls check the code. https://ide.codingblocks.com/s/104423
Rat chases cheese first 2 testcase
@urvigoel26
It seems you are trying for Rat in a maze problem.
Rat chases its cheese is a different problem in which the rat can move not just right and down but in all 4 directions. You have not considered the cases for rat moving up and left.
Make the required recursive calls for up and left direction as well and update your isSafe( ) accordingly.
Sir, I have made the respective changes but I am not getting output. Pls check the code https://ide.codingblocks.com/s/105694
@urvigoel26
Since the rat is now free to move in all 4 directions , it is very likely for your code to go in an infinite loop.
That is , the rat could go up , then down , then up , then down and so on continue infinitely and your code runs indefinitely.
To avoid this , we should check if the cell we are going to has already been visited or not , if it is visited , we do not proceed further . Since we are already marking our visited cells in “solarr” array , we can use it to check for this condition.
Make this small change in your issafe( ). Pass “solarr” as an argument to it.
Check
if( solarr [ i ][ j ] == 1) {
//If the cell has already been visited ,mark it unsafe
return false ;
}
Put this condition before your other condition in the issafe( ) and your code should work fine.
Try this and let me know if you need any help.