-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathLC1222.cpp
executable file
·46 lines (38 loc) · 1.08 KB
/
LC1222.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
39
40
41
42
43
44
45
46
/*
Problem Statement: https://leetcode.com/problems/queens-that-can-attack-the-king/
Time: O(n²)
Space: O(n)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
*/
class Solution {
public:
vector<vector<int>> queensAttacktheKing(vector<vector<int>>& queens, vector<int>& king) {
int n, qx, qy, ox, oy, kx, ky;
bool blocked, cond1, cond2;
vector<vector<int>> coords;
n = queens.size();
kx = king[0];
ky = king[1];
for (int i = 0; i < n; i++) {
blocked = false;
qx = queens[i][0];
qy = queens[i][1];
// check if queen can attack
if (qx != kx && qy != ky && abs(qx - kx) != abs(qy - ky))
continue;
// check if queen is being blocked
for (int j = 0; j < n && !blocked; j++) {
ox = queens[j][0];
oy = queens[j][1];
// check if points are collinear
if (j == i || (ky - oy) * (ox - qx) != (oy - qy) * (kx - ox))
continue;
else if ((min(qy, ky) < oy && oy < max(qy, ky)) || (min(qx, kx) < ox && ox < max(qx, kx)))
blocked = true;
}
if (!blocked)
coords.push_back(queens[i]);
}
return coords;
}
};