Escape a Large Maze LeetCode solution in C++

1
class Solution {
2
public:
3
vector<vector<int>> dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
4

5
bool isEscapePossible(vector<vector<int>> &blocked, vector<int> &source, vector<int> &target) {
6
unordered_set<long long> visited_s;
7
unordered_set<long long> visited_t;
8
for (auto &block : blocked) {
9
if (abs(source[0] - block[0]) + abs(source[1] - block[1]) <= 200)
10
visited_s.insert(((long long)block[0] << 32) + block[1]);
11
if (abs(target[0] - block[0]) + abs(target[1] - block[1]) <= 200)
12
visited_t.insert(((long long)block[0] << 32) + block[1]);
13
}
14
return bfs(source, target, visited_s, visited_s.size()) &&
15
bfs(target, source, visited_t, visited_t.size());
16
}
17
bool bfs(vector<int> &source, vector<int> &target, unordered_set<long long> &visited,
18
int blocks) {
19
queue<pair<int, int>> q;
20
q.push({source[0], source[1]});
21
while (!q.empty() && q.size() <= blocks) {
22
int x = q.front().first;
23
int y = q.front().second;
24
q.pop();
25
if (x == target[0] && y == target[1]) return true;
26
for (auto &dir : dirs) {
27
int next_x = x + dir[0];
28
int next_y = y + dir[1];
29
if (next_x < 0 || next_x > 999999 || next_y < 0 || next_y > 999999 ||
30
visited.find(((long long)next_x << 32) + next_y) != visited.end())
31
continue;
32
q.push({next_x, next_y});
33
visited.insert(((long long)next_x << 32) + next_y);
34
}
35
}
36
return !q.empty();
37
}
38
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0