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:
Function Description
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
Example 1:
Input: n = 4, m = 4, startRow = 0, startColumn = 0, endRow = 3, endColumn = 3, obstacleRow = [0, 1], obstacleColumn = [3, 2]
Output: 2
Explanation:Consider the following grid of size 4*4 where empty cells are free, 'S' indicates the start, 'E' indicates the end and 'X' indicates an obstcle:The optimal path is shown in the 'cell' column. Note, the cell coordinates are given as
(r, c)
wherer
is the index of the row andc
is the index of the column. Thus, the coordinates of any of the nearest obstacles is in 'Obstacle'. Coordinates of any of the nearest obstacles thus, the obstacles in the above example are located at(0, 3)
and(1,2)
. Coordinates of any of the nearest obstacles is in 'Obstacle'.* The blocked cell
(1, 2)
is also 3 units distant. Return 2, the closest distance to any obstacle along the path.
2 ≤ n, m ≤ 200
0 ≤ startRow, endRow < n
0 ≤ startColumn, endColumn < m
0 ≤ number of obstacles = n * m - 2
0 ≤ obstacleRow[i] < n
0 ≤ obstacleColumn[i] < m
- No obstacle at any start cell
- There is exactly one end cell
- There is at least one obstacle

input:
output: