Avoiding the Obstacles (IMC Sydney 🦪)
Problem statement
In this game, a player begins on a two-dimensional grid of size n × m. One cell of the grid is marked as the end and the player wants to reach this cell by moving up, down, left or right. However, some cells are occupied by obstacles. The goal of the player is to reach the end cell using a path that maximizes the minimum distance from any obstacle along that path.
The distance between any two points on the grid with coordinates (x1, y1) and (x2, y2) is calculated as |x1 - x2| + |y1 - y2|, where |a| is the absolute value of integer a.
Notes:
- One can visit a cell with an obstacle if necessary, i.e. no other path exists.
- A cell can be visited only once on a given path.
- The input start and end positions may be occupied by obstacles, and may coincide. If they coincide, the path contains only that cell.
Function
findBestPath(n: int, m: int, startRow: int, startColumn: int, endRow: int, endColumn: int, obstacleRow: int[], obstacleColumn: int[]) → int
Complete the function findBestPath in the editor below.
findBestPath has the following parameters:
int n: the number of rows in the gridint m: the number of columns in the gridint startRow: the row index of the starting positionint startColumn: the column index of the starting positionint endRow: the row index of the ending positionint endColumn: the column index of the ending positionint[] obstacleRow: the row index of the ith obstacleint[] obstacleColumn: the column index of the ith obstacle
Returns
int: the largest possible minimum distance from an obstacle in any path from the starting point to the ending point
Examples
Example 1
n = 4m = 4startRow = 0startColumn = 0endRow = 3endColumn = 3obstacleRow = [0, 1]obstacleColumn = [3, 2]return = 2
For the obstacles at (0, 3) and (1, 2), take the path (0, 0) → (1, 0) → (2, 0) → (3, 0) → (3, 1) → (3, 2) → (3, 3). The nearest-obstacle Manhattan distances along these seven cells are 3, 2, 3, 4, 3, 2, 3. The path’s minimum is 2. A threshold of 3 cannot connect the start to the end, so the maximum possible minimum distance is 2.
Example 2
n = 5m = 3startRow = 1startColumn = 1endRow = 4endColumn = 2obstacleRow = [0,2]obstacleColumn = [2,2]return = 2The obstacles are at (0, 2) and (2, 2). The path (1, 1) → (1, 0) → (2, 0) → (3, 0) → (4, 0) → (4, 1) → (4, 2) has nearest-obstacle distances 2, 3, 2, 3, 4, 3, 2, so its minimum is 2. Both endpoints have nearest-obstacle distance 2, which is an upper bound for every path; this path attains it.
Example 3
n = 3m = 3startRow = 0startColumn = 1endRow = 2endColumn = 2obstacleRow = [1,1,1]obstacleColumn = [0,1,2]return = 0Every path from the top row to the bottom row crosses the middle row, where all three cells contain obstacles. Such a cell has distance 0, and obstacle cells may be visited, so the maximum possible minimum distance is 0.
The formal endpoint is (2, 2); another diagram places E at (2, 1). Some photographs also print the final monster-column token 1 beside the coordinate annotation (1, 2). For this exercise, assume the final monster is at (1, 2), so this runnable example uses the annotated full row [0, 1, 2], consistent with the barrier diagram and stated output 0. The numeric discrepancy is preserved as a source transcription conflict.
Constraints
- For this combined-source exercise,
1 <= n, m <= 400. 0 <= startRow, endRow < n0 <= startColumn, endColumn < m1 <= obstacleRow.length = obstacleColumn.length <= n * m0 <= obstacleRow[i] < nand0 <= obstacleColumn[i] < m.- Obstacle coordinates are distinct.
- The start and end may be occupied by obstacles or coincide with each other.