
Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
For each cell (i,j), it is critical to find out the minimum number of steps to reach (i,j), denoted dis[i][j], quickly, given the tight constraint.
Calculate dis[i][j] going left to right, top to bottom.
Suppose we want to calculate dis[i][j], keep track of a priority queue that stores (dis[i][k], i, k) for all k ≤ j, and another priority queue that stores (dis[k][j], k, j) for all k ≤ i.