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 entrancedoes not count as an exit.
Return _thenumber of steps in the shortest path from the _entranceto the nearest exit, or-1if no such path exists.

Input: maze =[["+","+",".","+"],[".",".",".","+"],["+","+","+","."]], entrance =[1,2]Output: 1Explanation: There are 3 exits inthis 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 is1 step away.

Input: maze =[["+","+","+"],[".",".","."],["+","+","+"]], entrance =[1,0]Output: 2Explanation: There is1 exit inthis 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 is2 steps away.

Input: maze =[[".","+"]], entrance =[0,0]Output: -1Explanation: There are no exits inthis maze.
Use BFS starting from the entrance. For each cell, check its neighbors. If a neighbor is an empty cell and on the border (but not the entrance), return the number of steps. Mark visited cells to avoid cycles.
intnearestExit(vector<vector<char>>& maze, vector<int>& entrance) {
int m = maze.size(), n = maze[0].size();
queue<pair<int,int>> q; q.push({entrance[0], entrance[1]});
vector<vector<int>> vis(m, vector<int>(n, 0));
vis[entrance[0]][entrance[1]] =1;
int steps =0;
int dirs[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
while (!q.empty()) {
int sz = q.size();
while (sz--) {
auto [x, y] = q.front(); q.pop();
for (auto& d : dirs) {
int nx = x + d[0], ny = y + d[1];
if (nx <0|| ny <0|| nx >= m || ny >= n) continue;
if (maze[nx][ny] !='.'|| vis[nx][ny]) continue;
if ((nx ==0|| ny ==0|| nx == m-1|| ny == n-1) && (nx != entrance[0] || ny != entrance[1]))
return steps+1;
vis[nx][ny] =1; q.push({nx, ny});
}
}
steps++;
}
return-1;
}
publicintnearestExit(char[][] maze, int[] entrance) {
int m = maze.length, n = maze[0].length;
boolean[][] vis =newboolean[m][n];
Queue<int[]> q =new LinkedList<>();
q.offer(newint[]{entrance[0], entrance[1]});
vis[entrance[0]][entrance[1]]=true;
int steps = 0;
int[][] dirs = {{0,1},{0,-1},{1,0},{-1,0}};
while (!q.isEmpty()) {
int sz = q.size();
for (int i = 0; i < sz; i++) {
int[] p = q.poll();
int x = p[0], y = p[1];
for (int[] d : dirs) {
int nx = x + d[0], ny = y + d[1];
if (nx < 0 || ny < 0 || nx >= m || ny >= n) continue;
if (maze[nx][ny]!='.'|| vis[nx][ny]) continue;
if ((nx == 0 || ny == 0 || nx == m-1 || ny == n-1) && (nx != entrance[0]|| ny != entrance[1]))
return steps+1;
vis[nx][ny]=true; q.offer(newint[]{nx, ny});
}
}
steps++;
}
return-1;
}
funnearestExit(maze: Array<CharArray>, entrance: IntArray): Int {
val m = maze.size; val n = maze[0].size
val vis = Array(m) { BooleanArray(n) }
val dirs = arrayOf(intArrayOf(0,1), intArrayOf(0,-1), intArrayOf(1,0), intArrayOf(-1,0))
val q = ArrayDeque<Pair<Int,Int>>()
q.add(Pair(entrance[0], entrance[1]))
vis[entrance[0]][entrance[1]] = truevar steps = 0while (q.isNotEmpty()) {
repeat(q.size) {
val(x, y) = q.removeFirst()
for (d in dirs) {
val nx = x + d[0]; val ny = y + d[1]
if (nx !in0 until m || ny !in0 until n) continueif (maze[nx][ny] !='.'|| vis[nx][ny]) continueif ((nx ==0|| ny ==0|| nx == m-1|| ny == n-1) && (nx != entrance[0] || ny != entrance[1]))
return steps+1 vis[nx][ny] = true; q.add(Pair(nx, ny))
}
}
steps++ }
return -1}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from collections import deque
defnearestExit(maze, entrance):
m, n = len(maze), len(maze[0])
vis = [[False]*n for _ in range(m)]
q = deque([(entrance[0], entrance[1])])
vis[entrance[0]][entrance[1]] =True steps =0while q:
for _ in range(len(q)):
x, y = q.popleft()
for dx, dy in ((0,1),(0,-1),(1,0),(-1,0)):
nx, ny = x+dx, y+dy
if0<= nx < m and0<= ny < n and maze[nx][ny] =='.'andnot vis[nx][ny]:
if (nx ==0or ny ==0or nx == m-1or ny == n-1) and (nx != entrance[0] or ny != entrance[1]):
return steps+1 vis[nx][ny] =True q.append((nx, ny))
steps +=1return-1
use std::collections::VecDeque;
impl Solution {
pubfnnearest_exit(maze: Vec<Vec<char>>, entrance: Vec<i32>) -> i32 {
let m = maze.len(); let n = maze[0].len();
letmut vis =vec![vec![false; n]; m];
letmut q = VecDeque::new();
q.push_back((entrance[0] asusize, entrance[1] asusize));
vis[entrance[0] asusize][entrance[1] asusize] =true;
let dirs = [(0,1),(0,-1),(1,0),(-1,0)];
letmut steps =0;
while!q.is_empty() {
for _ in0..q.len() {
let (x, y) = q.pop_front().unwrap();
for (dx, dy) in dirs.iter() {
let nx = x asi32+ dx; let ny = y asi32+ dy;
if nx <0|| ny <0|| nx >= m asi32|| ny >= n asi32 { continue; }
let nx = nx asusize; let ny = ny asusize;
if maze[nx][ny] !='.'|| vis[nx][ny] { continue; }
if (nx ==0|| ny ==0|| nx == m-1|| ny == n-1) && (nx != entrance[0] asusize|| ny != entrance[1] asusize) {
return steps+1;
}
vis[nx][ny] =true; q.push_back((nx, ny));
}
}
steps +=1;
}
-1 }
}