-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queen's_Attack-II.cpp
121 lines (98 loc) · 2.8 KB
/
Queen's_Attack-II.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* Title : Queen's Attack II
* Author : Tridib Samanta
* Link : https://www.hackerrank.com/challenges/queens-attack-2/problem
**/
#include <bits/stdc++.h>
using namespace std;
int queensAttack(int n, int k, int r_q, int c_q, vector<vector<int>> obstacles) {
map<pair<int, int>, bool> obsPos;
for (int i = 0; i < k; ++i) {
obsPos[{obstacles[i][0], obstacles[i][1]}] = true;
}
int count = 0;
static int rowPos[] = {1, 1, 1, 0, -1, -1, -1, 0};
static int colPos[] = {-1, 0, 1, 1, 1, 0, -1, -1};
int currRow = 0, currCol = 0;
// NW
currRow = r_q + rowPos[0];
currCol = c_q + colPos[0];
while (currRow <= n && currCol >= 1 && !obsPos[{currRow, currCol}]) {
++count;
currRow += rowPos[0];
currCol += colPos[0];
}
// N
currRow = r_q + rowPos[1];
currCol = c_q + colPos[1];
while (currRow <= n && !obsPos[{currRow, currCol}]) {
++count;
currRow += rowPos[1];
currCol += colPos[1];
}
// NE
currRow = r_q + rowPos[2];
currCol = c_q + colPos[2];
while (currRow <= n && currCol <= n && !obsPos[{currRow, currCol}]) {
++count;
currRow += rowPos[2];
currCol += colPos[2];
}
// E
currRow = r_q + rowPos[3];
currCol = c_q + colPos[3];
while (currCol <= n && !obsPos[{currRow, currCol}]) {
++count;
currRow += rowPos[3];
currCol += colPos[3];
}
// SE
currRow = r_q + rowPos[4];
currCol = c_q + colPos[4];
while (currRow >= 1 && currCol <= n && !obsPos[{currRow, currCol}]) {
++count;
currRow += rowPos[4];
currCol += colPos[4];
}
// S
currRow = r_q + rowPos[5];
currCol = c_q + colPos[5];
while (currRow >= 1 && !obsPos[{currRow, currCol}]) {
++count;
currRow += rowPos[5];
currCol += colPos[5];
}
// SW
currRow = r_q + rowPos[6];
currCol = c_q + colPos[6];
while (currRow >= 1 && currCol >= 1 && !obsPos[{currRow, currCol}]) {
++count;
currRow += rowPos[6];
currCol += colPos[6];
}
// W
currRow = r_q + rowPos[7];
currCol = c_q + colPos[7];
while (currCol >= 1 && !obsPos[{currRow, currCol}]) {
++count;
currRow += rowPos[7];
currCol += colPos[7];
}
return count;
}
int main() {
int n, k;
cin >> n >> k;
int r_q, c_q;
cin >> r_q >> c_q;
vector<vector<int> > obstacles(k);
for (int i = 0; i < k; ++i) {
int r, c;
cin >> r >> c;
obstacles[i].emplace_back(r);
obstacles[i].emplace_back(c);
}
int res = queensAttack(n, k, r_q, c_q, obstacles);
cout << res;
return 0;
}