给你一个 m x n
的迷宫矩阵 maze
(下标从 0 开始),矩阵中有空格子(用 '.'
表示)和墙(用 '+'
表示)。同时给你迷宫的入口 entrance
,用 entrance = [entrancerow, entrancecol]
表示你一开始所在格子的行和列。
每一步操作,你可以往 上,下,左 或者 右 移动一个格子。你不能进入墙所在的格子,你也不能离开迷宫。你的目标是找到离 entrance
最近 的出口。出口 的含义是 maze
边界 上的 空格子。entrance
格子 不算 出口。
请你返回从 entrance
到最近出口的最短路径的 步数 ,如果不存在这样的路径,请你返回 -1
。
示例 1:
输入:maze = [["+","+",".","+"],[".",".",".","+"],["+","+","+","."]], entrance = [1,2] 输出:1 解释:总共有 3 个出口,分别位于 (1,0),(0,2) 和 (2,3) 。 一开始,你在入口格子 (1,2) 处。 - 你可以往左移动 2 步到达 (1,0) 。 - 你可以往上移动 1 步到达 (0,2) 。 从入口处没法到达 (2,3) 。 所以,最近的出口是 (0,2) ,距离为 1 步。
示例 2:
输入:maze = [["+","+","+"],[".",".","."],["+","+","+"]], entrance = [1,0] 输出:2 解释:迷宫中只有 1 个出口,在 (1,2) 处。 (1,0) 不算出口,因为它是入口格子。 初始时,你在入口与格子 (1,0) 处。 - 你可以往右移动 2 步到达 (1,2) 处。 所以,最近的出口为 (1,2) ,距离为 2 步。
示例 3:
输入:maze = [[".","+"]], entrance = [0,0] 输出:-1 解释:这个迷宫中没有出口。
提示:
maze.length == m
maze[i].length == n
1 <= m, n <= 100
maze[i][j]
要么是'.'
,要么是'+'
。entrance.length == 2
0 <= entrancerow < m
0 <= entrancecol < n
entrance
一定是空格子。
C++
class Solution {
public:
int nearestExit(vector<vector<char>>& maze, vector<int>& e) {
int m=maze.size();
int n=maze[0].size();
queue<pair<int,int>> que;
que.push(make_pair(e[0],e[1]));
map<pair<int,int>,int> tmp;
tmp[make_pair(e[0],e[1])]=1;
int count=0;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
while(!que.empty()) {
int k=que.size();
for(int i=0;i<k;i++) {
auto it=que.front();
que.pop();
if((it.first==0 || it.first==m-1 || it.second==0 || it.second==n-1) && !(it.first==e[0] && it.second==e[1])) {
return count;
}
for(int s=0;s<4;s++) {
int y=it.first+dy[s];
int x=it.second+dx[s];
if(y>=0 && y<m && x>=0 && x<n && 0==tmp[make_pair(y,x)] && '.'==maze[y][x]) {
tmp[make_pair(y,x)]=1;
que.push(make_pair(y,x));
}
}
}
count++;
}
return -1;
}
};
java
class Solution {
public int nearestExit(char[][] maze, int[] e) {
int m = maze.length;
int n = maze[0].length;
Map<Pair<Integer, Integer>, Integer> mp = new HashMap<>();
mp.put(new Pair<>(e[0], e[1]), 1);
Queue<Pair<Integer, Integer>> que = new LinkedList<>();
que.offer(new Pair<>(e[0], e[1]));
int count = 0;
int dy[] = {1, -1, 0, 0};
int dx[] = {0, 0, 1, -1};
while (!que.isEmpty()) {
int num = que.size();
for (int i = 0; i < num; i++) {
Pair<Integer, Integer> it = que.poll();
int r = it.getKey();
int c = it.getValue();
if ((r == 0 || r == m - 1 || c == 0 || c == n - 1) && !(r == e[0] && c == e[1])) {
return count;
}
for (int k = 0; k < 4; k++) {
int y = r + dy[k];
int x = c + dx[k];
if (y >= 0 && y < m && x >= 0 && x < n && '.' == maze[y][x] && !mp.containsKey(new Pair<>(y, x))) {
mp.put(new Pair<>(y, x), 1);
que.offer(new Pair<>(y, x));
}
}
}
count++;
}
return -1;
}
}
python
import queue
class Solution:
def nearestExit(self, maze: List[List[str]], e: List[int]) -> int:
m = len(maze)
n = len(maze[0])
ct = Counter()
ct[(e[0], e[1])] = 1
que = queue.Queue()
que.put((e[0], e[1]))
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
count = 0
while not que.empty():
num = que.qsize()
for i in range(num):
a = que.get()
if (a[0] == 0 or a[0] == m - 1 or a[1] == 0 or a[1] == n - 1) and not (a[0] == e[0] and a[1] == e[1]):
return count
for k in range(4):
y = a[0] + dy[k]
x = a[1] + dx[k]
if y >= 0 and y < m and x >= 0 and x < n and '.' == maze[y][x] and 0 == ct[(y, x)]:
ct[(y, x)] = 1
que.put((y, x))
count += 1
return -1