Please check code for Rat chases its cheeze problem

Hello @sahilkhan2312000131 i have made all the necessary changes;

Can you please explain why are we first checking if the point up,below,right,left to current position is 0 and then sending pointer to that position whereas in Rat in maze problem we sent to pointer to next point and then working happens.

@sahilkhan2312000131 this because in rat in a maze problem we were only moving towards right and down whereas in this the answer can be calculated for all the four directions.
But if we will give 4 calls like the you were giving then the complexity will be exponential to the 4 so in order to control that we will make a check for the valid cases.

@tarunluthra
@rhlbhrdwj3

pls check …

@sahilkhan2312000131 @tusharaggarwal272
The overall complexity is still going to remain same despite making the checks.
The reason we are exploring all 4 paths is simply because it is said so in the problem statement.
Rat in a maze problem specifies that the rat is allowed to move only right and down. Rat chases its cheese problem specifies that the rat is allowed to move in all 4 directions hence we have to make 4 recursive calls to accomodate that.
The if checks implemented ensure that we do not end in an infinite recursive cycle. Remove those if checks and the judge will give you a runtime error while if you run that code on your local compiler, you would not get any output and the program would keep on running as it is a case of infinite recursion i.e. we are never hitting a base case.

1 Like

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.