leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1222.cpp (827B)


0 class Solution {
1 public:
2 vector<vector<int>> queensAttacktheKing(vector<vector<int>> &queens, vector<int> &king) {
3 static constexpr const int offset_x[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
4 static constexpr const int offset_y[8] = {0, 1, 1, 1, 0, -1, -1, -1};
6 int hit[64] = {0};
7 vector<vector<int>> res;
9 for (const auto &queen : queens)
10 hit[queen[0] * 8 + queen[1]] = true;
12 for (int i = 0; i < 8; i++) {
13 int x = king[0] + offset_x[i], y = king[1] + offset_y[i];
14 while (x >= 0 && x < 8 && y >= 0 && y < 8) {
15 if (hit[x * 8 + y]) {
16 res.push_back({x, y});
17 break;
18 }
19 x += offset_x[i], y += offset_y[i];
20 }
21 }
23 return res;
24 }
25 };