← 返回 uber 的题目列表Nearest Exit in a 2D Grid (BFS)
类型:online_judge
Given a 2D grid grid where:
'.' is an empty cell
'+' is a wall and an entrance coordinate entrance = (sr, sc) with grid[sr][sc] == '.'.
An exit is any empty cell on the boundary of the grid that is not the entrance.
Return the minimum number of steps from the entrance to the nearest exit using 4-direction moves, or -1 if no exit exists.
Input
grid: R x C character matrix
entrance: two integers sr sc
Output
minimum steps (int) or -1
Constraints
1 <= R, C <= 200
4-directional movement only
Examples
(see CN section for 5 sample cases)
Example
Input
3 3
+ + .
. . .
+ + +
1 0
Output
2