Why is not the code correct

The code is giving correct ans for the sample input]why is is not correct.

@Codarikh
Your code is printing multiple possible configurations. You are only required to print the first possible valid configuration as per the problem.

how should I do that

After you make the recursive call in Line 23 for the right part , check whether it returned true or false. If it returns true , i.e. return true from there only. However if r is false , then make the recursive call for down part. This will ensure that if we found a valid path earlier , we wouldn’t go to find another one without checking.