Problem
You get a maze as an m x n matrix maze. Each cell is an empty cell "." or a wall "+". You also get entrance = [row, col], the empty cell where you start.
In one step, you move to the cell above, below, left or right of your cell. You cannot move into a wall or out of the maze.
An exit is an empty cell on the border of the maze. The border is the first row, the last row, the first column and the last column. The entrance is not an exit, even when it is on the border.
Return the number of steps in the shortest path from the entrance to the nearest exit. If you cannot reach an exit, return -1.
Example 1
- Input:
- maze = [["+","+",".","+"],[".",".",".","+"],["+","+","+","."]], entrance = [1,2]
- Output:
- 1
- Explanation:
- The cell
[0, 2]is an exit, one step above the entrance. The exit[1, 0]is two steps away.
Example 2
- Input:
- maze = [["+","+","+"],[".",".","+"],["+",".","+"]], entrance = [1,0]
- Output:
- 2
- Explanation:
- The entrance is on the border, but it is not an exit. The nearest exit is
[2, 1].
Example 3
- Input:
- maze = [[".","+"]], entrance = [0,0]
- Output:
- -1
Constraints
- maze.length == m
- maze[i].length == n
- 1 <= m, n <= 100
- maze[i][j] is "." or "+".
- entrance.length == 2
- 0 <= row < m
- 0 <= col < n
- entrance is an empty cell.
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
Sources · 1 reference
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 2 records
Meta
Leetcode 1926. Nearest Exit from Entrance in Maze
Reported Mar 1, 2026 · 2 source reports
SENIOR
Source snapshot: Nov 12, 2026
View sourceTikTok
Leetcode 1926. Nearest Exit from Entrance in Maze
Reported Apr 23, 2026 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026
View source