You are lost in a strange land and have to return to the base camp as soon as possible. The land is modeled as a rectangular plane divided into square cells, and each cell can be of one of 9 different types, numbered from 1 to 9. The cost of a cell of type k is exactly 1/k.
To get to the base camp, you can move from each cell to any of its orthogonally adjacent cells (up, down, left or right) instantaneously. The only limitation for the speed of your movements is the cost-per-minute. Time will be divided into one minute intervals, i.e., there is an interval starting at time 0 and ending at time 1, another interval from time 1 to time 2, etc. During each interval of time, the sum of the costs of all the cells you occupy must not exceed 1. If you make an instantaneous move at the exact boundary between two intervals of time, the cells you move between during that move will count toward both intervals' total costs. This means you can never step into or out of a cell of type 1 because any other cell you occupy will always exceed the maximum cost per minute.
You will be given m, a String[] describing the land, where the jth character of the ith element represents the type of the cell at row i, column j (both 1-based). You will also be given startRow, startCol, endRow and endCol, the 1-based indices of the row and column of your starting point and destination, respectively. Return the minimum number of minutes that are needed to get from (startRow, startCol) to (endRow, endCol) following the constraints above. If it's impossible to do so, return -1.
|