代码拉取完成,页面将自动刷新
// Time: O(n^2), n is the number of blocked
// Space: O(n)
class Solution {
public:
template <typename T>
struct PairHash {
size_t operator()(const pair<T, T>& p) const {
size_t seed = 0;
seed ^= std::hash<T>{}(p.first) + 0x9e3779b9 + (seed<<6) + (seed>>2);
seed ^= std::hash<T>{}(p.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
return seed;
}
};
bool isEscapePossible(vector<vector<int>>& blocked, vector<int>& source, vector<int>& target) {
unordered_set<pair<int, int>, PairHash<int>> blocks;
for (const auto& block : blocked) {
blocks.emplace(block[0], block[1]);
}
return bfs(blocks, make_pair(source[0], source[1]), make_pair(target[0], target[1])) &&
bfs(blocks, make_pair(target[0], target[1]), make_pair(source[0], source[1]));
}
private:
bool bfs(const unordered_set<pair<int, int>, PairHash<int>>& blocks,
const pair<int, int>& source, const pair<int, int>& target) {
static const int R = 1e6;
static const int C = 1e6;
static const vector<pair<int, int>> directions{{0, -1}, {0, 1},
{-1, 0}, {1, 0}};
const auto& max_area_surrounded_by_blocks = blocks.size() * (blocks.size() - 1) / 2;
unordered_set<pair<int, int>, PairHash<int>> lookup{source};
if (lookup.size() > max_area_surrounded_by_blocks) {
return true;
}
queue<pair<int, int>> q({source});
while (!q.empty()) {
auto source = q.front(); q.pop();
if (source == target) {
return true;
}
for (const auto& dir : directions) {
const auto nr = source.first + dir.first, nc = source.second + dir.second;
if (!((0 <= nr && nr < R) &&
(0 <= nc && nc < C) &&
!lookup.count(make_pair(nr, nc)) &&
!blocks.count(make_pair(nr, nc)))) {
continue;
}
lookup.emplace(nr, nc);
if (lookup.size() > max_area_surrounded_by_blocks) {
return true;
}
q.emplace(nr, nc);
}
}
return false;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。