1
class Solution {
2
public:
3
vector<vector<int>> queensAttacktheKing(vector<vector<int>> &queens, vector<int> &king) {
4
vector<vector<int>> ans;
5
vector<vector<int>> board(8, vector<int>(8, 0));
6

7
for (auto queen : queens) board[queen[0]][queen[1]] = 1;
8

9
for (int x = -1; x <= 1; x++) // Both loops are for checking in all the 8 possible directions
10
{
11
for (int y = -1; y <= 1; y++) {
12
if (x == 0 and y == 0) continue;
13
int startx = king[0], starty = king[1];
14

15
while (startx >= 0 and startx < 8 and starty >= 0 and starty < 8) {
16
if (board[startx][starty] == 1) // If queen is found, append it to ans and break
17
{
18
ans.push_back({startx, starty});
19
break;
20
}
21
startx += x,
22
starty += y; // Otherwise keep moving forward in earlier direction
23
}
24
}
25
}
26
return ans;
27
}
28
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0