1926. Nearest Exit from Entrance in Maze LeetCode Solution
In this guide, you will get 1926. Nearest Exit from Entrance in Maze LeetCode Solution with the best time and space complexity. The solution to Nearest Exit from Entrance in Maze problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Nearest Exit from Entrance in Maze solution in C++
- Nearest Exit from Entrance in Maze solution in Java
- Nearest Exit from Entrance in Maze solution in Python
- Additional Resources

Problem Statement of Nearest Exit from Entrance in Maze
You are given an m x n matrix maze (0-indexed) with empty cells (represented as ‘.’) and walls (represented as ‘+’). You are also given the entrance of the maze, where entrance = [entrancerow, entrancecol] denotes the row and column of the cell you are initially standing at.
In one step, you can move one cell up, down, left, or right. You cannot step into a cell with a wall, and you cannot step outside the maze. Your goal is to find the nearest exit from the entrance. An exit is defined as an empty cell that is at the border of the maze. The entrance does not count as an exit.
Return the number of steps in the shortest path from the entrance to the nearest exit, or -1 if no such path exists.
Example 1:
Input: maze = [[“+”,”+”,”.”,”+”],[“.”,”.”,”.”,”+”],[“+”,”+”,”+”,”.”]], entrance = [1,2]
Output: 1
Explanation: There are 3 exits in this maze at [1,0], [0,2], and [2,3].
Initially, you are at the entrance cell [1,2].
– You can reach [1,0] by moving 2 steps left.
– You can reach [0,2] by moving 1 step up.
It is impossible to reach [2,3] from the entrance.
Thus, the nearest exit is [0,2], which is 1 step away.
Example 2:
Input: maze = [[“+”,”+”,”+”],[“.”,”.”,”.”],[“+”,”+”,”+”]], entrance = [1,0]
Output: 2
Explanation: There is 1 exit in this maze at [1,2].
[1,0] does not count as an exit since it is the entrance cell.
Initially, you are at the entrance cell [1,0].
– You can reach [1,2] by moving 2 steps right.
Thus, the nearest exit is [1,2], which is 2 steps away.
Example 3:
Input: maze = [[“.”,”+”]], entrance = [0,0]
Output: -1
Explanation: There are no exits in this maze.
Constraints:
maze.length == m
maze[i].length == n
1 <= m, n <= 100
maze[i][j] is either '.' or '+'.
entrance.length == 2
0 <= entrancerow < m
0 <= entrancecol < n
entrance will always be an empty cell.
Complexity Analysis
- Time Complexity: O(mn)
- Space Complexity: O(mn)
1926. Nearest Exit from Entrance in Maze LeetCode Solution in C++
class Solution {
public:
int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {
constexpr int dirs[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
const int m = maze.size();
const int n = maze[0].size();
queue<pair<int, int>> q{{{entrance[0], entrance[1]}}};
vector<vector<bool>> seen(m, vector<bool>(n));
seen[entrance[0]][entrance[1]] = true;
for (int step = 1; !q.empty(); ++step)
for (int sz = q.size(); sz > 0; --sz) {
const auto [i, j] = q.front();
q.pop();
for (const auto& [dx, dy] : dirs) {
const int x = i + dx;
const int y = j + dy;
if (x < 0 || x == m || y < 0 || y == n)
continue;
if (seen[x][y] || maze[x][y] == '+')
continue;
if (x == 0 || x == m - 1 || y == 0 || y == n - 1)
return step;
q.emplace(x, y);
seen[x][y] = true;
}
}
return -1;
}
};
/* code provided by PROGIEZ */
1926. Nearest Exit from Entrance in Maze LeetCode Solution in Java
class Solution {
public int nearestExit(char[][] maze, int[] entrance) {
final int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
final int m = maze.length;
final int n = maze[0].length;
Queue<Pair<Integer, Integer>> q =
new ArrayDeque<>(List.of(new Pair<>(entrance[0], entrance[1])));
boolean[][] seen = new boolean[m][n];
seen[entrance[0]][entrance[1]] = true;
for (int step = 1; !q.isEmpty(); ++step)
for (int sz = q.size(); sz > 0; --sz) {
final int i = q.peek().getKey();
final int j = q.poll().getValue();
for (int[] dir : dirs) {
final int x = i + dir[0];
final int y = j + dir[1];
if (x < 0 || x == m || y < 0 || y == n)
continue;
if (seen[x][y] || maze[x][y] == '+')
continue;
if (x == 0 || x == m - 1 || y == 0 || y == n - 1)
return step;
q.offer(new Pair<>(x, y));
seen[x][y] = true;
}
}
return -1;
}
}
// code provided by PROGIEZ
1926. Nearest Exit from Entrance in Maze LeetCode Solution in Python
class Solution:
def nearestExit(self, maze: list[list[str]], entrance: list[int]) -> int:
dirs = ((0, 1), (1, 0), (0, -1), (-1, 0))
m = len(maze)
n = len(maze[0])
q = collections.deque([(entrance[0], entrance[1])])
seen = {(entrance[0], entrance[1])}
step = 1
while q:
for _ in range(len(q)):
i, j = q.popleft()
for dx, dy in dirs:
x = i + dx
y = j + dy
if x < 0 or x == m or y < 0 or y == n:
continue
if (x, y) in seen or maze[x][y] == '+':
continue
if x == 0 or x == m - 1 or y == 0 or y == n - 1:
return step
q.append((x, y))
seen.add((x, y))
step += 1
return -1
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.