Nearest Other Exit on the Board Edge
Problem statement
A rectangular board contains 0 for passable cells and + for obstacles. Given a passable entry cell [startRow,startCol] on the board edge, return the closest other reachable exit on the board edge.
For this exercise, assume one move steps up, down, left or right to a passable cell and costs one unit. An exit is any passable cell in the first or last row or column, except the entry itself. Choose a reachable exit using the fewest moves. Among equally close exits, choose the smallest row index, then the smallest column index.
For this exercise, assume coordinates are zero-based and the result is [row,col]. Return [-1,-1] when there is no other reachable exit. The board is not modified.
Function
nearestEdgeExit(grid: String[], startRow: int, startCol: int) → int[]Examples
Example 1
grid = ["+0+","000","+0+"]startRow = 0startCol = 1return = [1,0]The two side exits are both two moves away; row/column order chooses [1,0].
Example 2
grid = ["+0+++","+000+","+++0+","+++0+"]startRow = 0startCol = 1return = [3,3]The only other reachable edge cell is [3,3], reached in five orthogonal moves.
Example 3
grid = ["0"]startRow = 0startCol = 0return = [-1,-1]The entry itself is excluded, so this board has no other exit.
Constraints
- For this exercise, assume
1 <= grid.length <= 30and each row has the same length from1through30. - Every cell is
0or+. - For this exercise, assume the entry indices are in range, the entry contains
0, and it lies on the first or last row or column.