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