my code prints all the possible solutions
but sample output demands one
Rat in a maze problem
@Bansaljatin05
You should check the result of your first recursive call before making the second one.
bool RightSuccess=CanSolve(Maze,Sol,i+1,j,m,n);
if(RightSuccess)
return true;
bool DownSuccess=CanSolve(Maze,Sol,i,j+1,m,n);
Otherwise even though you had a correct configuration printed through the first call , your code still goes on to make another recursive call to print more such configurations.
@Bansaljatin05
2 mistakes in your code.
- You missed the backtracking step.
- You are required to print the rightmost path in the given problem. If you look closely you will realise that you are actually calling downwards in your RightSuccess call and rightwards in your DownSuccess call , i.e. your recursive calls are inverted. Since you should print the rightmost possible path , the first call should be for the rightwards movement however your first call goes for the downwards side. Hence the wrong output.
Correct these two and your code should work fine.
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.
TESTCASE # 1 : no-output (Time: 0 s)
TESTCASE # 2 : correct (Time: 0 s)
TESTCASE # 3 : no-output (Time: 0.03 s)
TESTCASE # 4 : no-output (Time: 0 s)
TESTCASE # 5 : timelimit (Time: 5.96 s)
TESTCASE # 6 : correct (Time: 0.13 s)
@Bansaljatin05
Line No. 26
Compare i with n instead of m.
Also you are required to print -1 in case no path exists. You have not included a case for that.