Nearest Exit from Entrance in Maze
Run a breadth-first search from the entrance. It visits cells in rings of equal distance: all cells 1 step away, then all cells 2 steps away, and so on. The first border cell it pops is the nearest exit, so the search can stop there.
A cell is popped only after every cell that is closer to the entrance, so the first border cell popped is the nearest exit. The entrance is 0 steps away and is never counted.
Problem
You are given a maze as a grid. Each cell is an empty cell (.) or a wall (+). You are also given the entrance, an empty cell where you start.
In one step you can move up, down, left or right into an empty cell. An exit is an empty cell on the border of the maze. The entrance does not count as an exit, even when it sits on the border.
Return the number of steps to the nearest exit, or -1 if no exit can be reached.
maze, an m × n grid of . and +, and entrance = [row, col], an empty cell.
The number of steps to the nearest reachable exit, or -1 if there is none.
The exit at [0, 2] is one step up from the entrance. The other exits are farther away, or walled off.
The entrance is on the left border, but it is not an exit. The only exit is [1, 2], two steps to the right.
There is no exit. The only other cell is a wall.
- • 1 ≤ m, n ≤ 100
- • Each cell is `.` (empty) or `+` (wall).
- • The entrance is always an empty cell.
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
Is the answer the length of any path to an exit, or the length of the shortest one?
- 2.
If I try all four directions from the entrance at the same time, which border cell can I reach first?
- 3.
Does the entrance count as an exit when it sits on the border?
- 4.
If a depth-first search finds an exit, is that exit guaranteed to be the nearest one?
- 5.
After k rounds of a search, which cells are known to be exactly k steps away?
Brute force approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Walk every simple path from the entrance; keep the shortest one that reaches an exit | O(3^(m · n)) | O(m · n) |
| Optimal | BFS by distance from the entrance; the first border cell popped is the nearest exit | O(m · n) | O(m · n) |
Why does this belong to this pattern?
- “steps from the entrance”Every move costs the same, so the distance is the number of moves. A BFS gives the shortest distance to every cell.
- “nearest exit”Stop at the first exit found in distance order, not at any exit the search happens to reach.
- “empty cells and walls in a grid”The grid is a graph: each empty cell is a node and each move is an edge.
This is the anchor of the grid-shortest sub-pattern. Every move costs one step, so BFS gives the true distance to every cell. The only twist is the stop rule: the first border cell popped is the answer, not the first one a DFS happens to reach. The next two questions keep the BFS and change what counts as a step or a state.
Don't make this mistake
The second example's entrance [1, 0] is on the left border. Without the steps > 0 check the first pop returns 0, but the correct answer is 2. The entrance is never an exit.
On the dry-run maze a DFS that tries up, down, left, right first walks to a border cell 5 steps away. The nearest exit is 3 steps away at (1,4). A depth-first search follows one path to its end, so the first exit it reaches is not the closest.
Take the maze ++++ / +..+ / +..+ / ++++ with the entrance at (1,1). The four interior cells form a loop with no border cell in it, so without seen the queue keeps refilling forever and the search never ends. With seen the queue empties and the answer is -1.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Graph BFS / DFS translate into production architectures and low-level memory mechanics.