Nearest Reachable Grid Corner
Learn this problemProblem statement
You are given a non-empty rectangular grid where 0 is passable and + is impassable. The passable starting cell (startRow, startCol) is one of the grid's geometric corners.
Moving one step up, down, left, or right through passable cells costs one. Find a different passable geometric corner reachable with minimum path distance. If several destinations have the same minimum distance, return the lexicographically smallest coordinate: smallest row, then smallest column.
Return the destination as [row, column], or [-1] if no different corner is reachable. Repeated coordinates in a one-row or one-column grid represent one geometric corner.
Function
nearestReachableCorner(grid: String[], startRow: int, startCol: int) → int[]Examples
Example 1
grid = ["000","+0+","000"]startRow = 0startCol = 0return = [0,2]The top-right corner is reachable in two steps, while the bottom-left and bottom-right corners require longer paths.
Example 2
grid = ["0+","++"]startRow = 0startCol = 0return = [-1]Every other geometric corner is impassable, so no destination is reachable.
Constraints
1 <= grid.length.1 <= grid[i].length.- Every row has the same length and the grid contains at most
4,096cells. - Every cell is
0or+. (startRow, startCol)is a geometric corner and is passable.