-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsolution.cpp
35 lines (35 loc) · 1.18 KB
/
solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* 194 / 194 test cases passed.
* Runtime: 108 ms
* Memory Usage: 32.2 MB
*/
class Solution {
public:
int direct[4][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
using pii = pair<int, int>;
int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {
int m = maze.size(), n = maze[0].size();
vector<vector<int>> used(m, vector<int>(n));
queue<pii> que;
que.emplace(entrance[0], entrance[1]);
used[entrance[0]][entrance[1]] = true;
int path = 0;
while (!que.empty()) {
int level = que.size();
for (int i = 0; i < level; ++ i) {
auto [x, y] = que.front(); que.pop();
for (auto &d : direct) {
int xx = x + d[0];
int yy = y + d[1];
if (xx < 0 || xx >= m || yy < 0 || yy >= n) continue;
if (maze[xx][yy] == '+' || used[xx][yy]) continue;
if (xx == 0 || xx == m - 1 || yy == 0 || yy == n - 1) return path + 1;
used[xx][yy] = true;
que.emplace(xx, yy);
}
}
++ path;
}
return -1;
}
};