Skip to content
Problem 1926 · LeetCode 1926(opens in a new tab)

Nearest Exit from Entrance in Maze

MediumBreadth-first search

You get a maze as an m × n grid maze. Each cell is either "." (open) or "+" (a wall). You also get entrance = [row, col], an open cell where you stand.

One move takes you to the open cell directly above, below, left of or right of you. Walls block you, and so does the edge of the grid. An exit is any open cell on the outer edge of the grid, but the entrance itself never counts as one.

Return the fewest steps needed to reach an exit, or −1 if no exit can be reached.

Example 1

Input:  maze = ["+++++",
                "+...+",
                "..+.+",
                "+.+.+",
                "+++.+"],  entrance = [1, 2]
Output: 3
Why:    left, down, left reaches (2,0); the exit at (4,3) takes 4
2 more examples and the constraints

Example 2

Input:  maze = ["+++", "+.+", "+++"],  entrance = [1, 1]
Output: -1
Why:    the entrance is boxed in by walls

Example 3

Input:  maze = ["..."],  entrance = [0, 0]
Output: 1
Why:    the entrance is on the edge but does not count; (0,1) does

Constraints

  • 1 ≤ m, n ≤ 100
  • Each cell is "." or "+"
  • entrance is inside the grid and is an open cell

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Sweep until distances settleO((m·n)²)
  2. Breadth-first searchO(m·n)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for a winding 100 × 100 mazeVerdict
Sweep until distances settleO((m·n)²)
Correct; many sweeps on winding mazes
Breadth-first searchO(m·n)
Best; each cell once, stops at the first exit

m × n = size of the maze.

Edge cases to test

  • Entrance on the border. It is not an exit itself, as in Example 3, so the answer is at least 1.
  • Walled in. No open neighbour at all gives −1.
  • One cell. [["."]] with entrance [0, 0] has no other cell, so the answer is −1.
  • Exit unreachable. Open border cells behind walls do not help; the answer is −1 if BFS runs out.

Common mistakes

  • Counting the entrance as an exit when it sits on the border.
  • Marking cells when they are popped instead of pushed, so the same cell is queued many times.
  • Using depth-first search, which finds a path but not the shortest one.

How to talk through it

  1. 1Clarify. Does the entrance count as an exit? (No.) Can I change the maze to mark cells?
  2. 2Name the model. Cells are nodes, moves are edges of equal weight: shortest path means BFS.
  3. 3Explain the stop. BFS reaches cells in order of distance, so the first exit seen is the nearest.
  4. 4Code it. Queue of (row, col, steps), mark on push, return on the first border cell.
  5. 5Test out loud. Run a border entrance, a walled-in entrance and a one-cell maze.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?