This is a hackerearth question.
Question LINK
Please dont share code link, i need to know the concept.
i updated it. i lost the fight sadly
Let the source row and destination row be a and b (a<b). Obviously we need to travel along rows from a to b (or from a to 1 and then 1 to n and then n to b). The one which is minimum is the optimal way to reach from row a to b. Repeat the same for columns say c and d.
Shortest path length = [ min(abs(a-b),n-abs(a-b)) + min(abs(c-d),m-abs(c-d)) ]
Cost = Shortest path length * p.
1 Like