forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queens-that-can-attack-the-king.cpp
38 lines (36 loc) · 1.2 KB
/
queens-that-can-attack-the-king.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
36
37
38
// Time: O(1)
// Space: O(1)
class Solution {
private:
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;
}
};
public:
vector<vector<int>> queensAttacktheKing(vector<vector<int>>& queens, vector<int>& king) {
static const vector<pair<int, int>> directions =
{{-1, 0}, {0, 1}, {1, 0}, {0, -1},
{-1, 1}, {1, 1}, {1, -1}, {-1, -1}};
vector<vector<int>> result;
unordered_set<pair<int, int>, PairHash<int>> lookup;
for (const auto& queen : queens) {
lookup.emplace(queen[0], queen[1]);
}
for (const auto& [dx, dy] : directions) {
for (int i = 1; i <= 7; ++i) {
int x = king[0] + dx * i;
int y = king[1] + dy * i;
if (lookup.count(make_pair(x, y))) {
result.push_back({x, y});
break;
}
}
}
return result;
}
};