Back to problems

Nearest Exit in a 2D Grid (BFS)

Algorithm · Uber · Medium

You are given a two-dimensional character grid grid in which: '.' marks an open square. '+' marks an obstacle. You also receive an entrance location entrance = (sr, sc), and grid[sr][sc] is guaranteed to be '.'. An exit is an open square located along the grid's outer edge, except that the entrance square itself cannot be considered an exit. Using only moves up, down, left, or right, return the fewest moves needed to reach any exit from the entrance. Return -1 when no exit…

Checking your access…