-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2257.cpp
More file actions
162 lines (155 loc) · 6.92 KB
/
2257.cpp
File metadata and controls
162 lines (155 loc) · 6.92 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
class Solution {
public:
int upperbound(vector<int>& arr, int x) {
int left = 0;
int right = arr.size();
while (left < right) {
int mid = left + (right - left) / 2;
if (arr[mid] > x) right = mid;
else left = mid + 1;
}
return left;
}
int countUnguarded(int m, int n, vector<vector<int>>& guards, vector<vector<int>>& walls) {
unordered_map<int, vector<int>> guardInRow;
unordered_map<int, vector<int>> guardInCol;
unordered_map<int, vector<int>> wallInRow;
unordered_map<int, vector<int>> wallInCol;
vector<vector<bool>> unavailable(m, vector<bool>(n, false));
for (auto guard : guards) {
guardInRow[guard[0]].push_back(guard[1]);
guardInCol[guard[1]].push_back(guard[0]);
unavailable[guard[0]][guard[1]] = true;
}
for (auto wall : walls) {
wallInRow[wall[0]].push_back(wall[1]);
wallInCol[wall[1]].push_back(wall[0]);
unavailable[wall[0]][wall[1]] = true;
}
for (auto& element : guardInRow) sort(element.second.begin(), element.second.end());
for (auto& element : guardInCol) sort(element.second.begin(), element.second.end());
for (auto& element : wallInRow) sort(element.second.begin(), element.second.end());
for (auto& element : wallInCol) sort(element.second.begin(), element.second.end());
int res = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
// is guard or wall
if (unavailable[i][j]) continue;
// check left and right
if (guardInRow.find(i) != guardInRow.end()) {
if (wallInRow.find(i) == wallInRow.end()) {
// has guard but no wall
continue;
}
else {
// has guard and wall
int rG = upperbound(guardInRow[i], j);
int lG = rG - 1;
// only left guard
if (rG == guardInRow[i].size()) {
// no wall > guard
int index = upperbound(wallInRow[i], guardInRow[i][lG]);
if (index == wallInRow[i].size()) {
continue;
}
// wall > col
if (wallInRow[i][index] > j) continue;
}
// only right
else if (lG == -1) {
// find wall right to the curr pos
int index = upperbound(wallInRow[i], j);
if (index == wallInRow[i].size() || wallInRow[i][index] > guardInRow[i][rG]) {
continue;
}
}
// middle
else {
// more than left guard
int indexRightToLeftGuard = upperbound(wallInRow[i], guardInRow[i][lG]);
int indexRightToCurrCol = upperbound(wallInRow[i], j);
if (indexRightToLeftGuard == wallInRow[i].size() || indexRightToCurrCol == wallInRow[i].size()) continue;
if (wallInRow[i][indexRightToLeftGuard] > j || wallInRow[i][indexRightToCurrCol] > guardInRow[i][rG]) continue;
}
}
}
// check up and down
if (guardInCol.find(j) != guardInCol.end()) {
if (wallInCol.find(j) == wallInCol.end()) {
// has guard but no wall
continue;
}
else {
// has guard and wall
// down > up
int downG = upperbound(guardInCol[j], i);
int upG = downG - 1;
// only up guard
if (downG == guardInCol[j].size()) {
// no wall > guard
int index = upperbound(wallInCol[j], guardInCol[j][upG]);
if (index == wallInCol[j].size()) {
continue;
}
if (wallInCol[j][index] > i) continue;
}
// only down
else if (upG == -1) {
// find wall down to the curr pos
int index = upperbound(wallInCol[j], i);
if (index == wallInCol[j].size() || wallInCol[j][index] > guardInCol[j][downG]) {
continue;
}
}
// middle
else {
// more than up guard
int indexDownToUpGuard = upperbound(wallInCol[j], guardInCol[j][upG]);
int indexDownToCurrCol = upperbound(wallInCol[j], i);
if (indexDownToUpGuard == wallInCol[j].size() || indexDownToCurrCol == wallInCol[j].size()) continue;
if (wallInCol[j][indexDownToUpGuard] > i || wallInCol[j][indexDownToCurrCol] > guardInCol[j][downG]) continue;
}
}
}
res += 1;
}
}
return res;
}
};
class Solution {
public:
vector<pair<int, int>> directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int countUnguarded(int m, int n, vector<vector<int>>& guards, vector<vector<int>>& walls) {
vector<vector<char>> grid(m, vector<char>(n, 'E'));
for (auto& wall : walls) {
grid[wall[0]][wall[1]] = 'W';
}
for (auto& guard : guards) {
grid[guard[0]][guard[1]] = 'G';
}
for (auto& guard : guards) {
for (auto& direction : directions) {
int x = guard[0];
int y = guard[1];
while (true) {
int nextX = x + direction.first;
int nextY = y + direction.second;
if (nextX < 0 || nextX >= m || nextY < 0 || nextY >= n) break;
if (grid[nextX][nextY] == 'W') break;
if (grid[nextX][nextY] == 'G') break;
grid[nextX][nextY] = 'P';
x = nextX;
y = nextY;
}
}
}
int res = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 'E') res++;
}
}
return res;
}
};