May be this can help.
ok this thing would have worked if we made an matrix of constant size?
That person says so… you can try (I’m not sure and away from my laptop).
Thankyou for the information
Thanks for the information
https://www.codechef.com/viewsolution/35761019
please help me to find a test case for which my solution is giving wrong answer
i didn’t get this reachable thing
I am assuming that you have understood how this problem has been reduced to finding the lexicographical path from (1,1) to (N,M) in a grid. So, the thing is that you can include a point in your answer for the lexicographically smallest, only if that point can reach to the end point that is (N,M). This can also be written as checking whether the point is reachable from (N,M) or not because if (N,M) is reachable from (I,J) then (I,J) will be reachable from N,M on applying the reverse path. So, the idea is to run a single DFS from (N,M) to check for every point in O(NM) rather than doing it separately for every point which will require O(NMNM)