-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAntsMeet.cpp
213 lines (184 loc) · 5.99 KB
/
AntsMeet.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <iostream>
#include <iterator>
#include <algorithm>
#include <map>
#include <vector>
#include <iomanip>
#include <sstream>
#include <queue>
#include <string>
#include <cmath>
#include <set>
#include <limits>
#include <numeric>
#include <bitset>
#include <stack>
#include <utility>
#include <unordered_set>
#include <unordered_map>
#define DEBUG 0
#define ARR_SIZE(x) ((sizeof(x)) / (sizeof(x[0])))
#define INF_INT numeric_limits<int>::max()
#define MIN_INT numeric_limits<int>::min()
using namespace std;
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> pii;
vector<string> &split(const string &s, char delim, vector<string> &elems) {
stringstream ss(s);
string item;
while (getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
vector<string> split(const string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
return elems;
}
template <typename K, typename V>
V GetWithDef(const std::map <K,V> & m, const K & key, const V & defval ) {
typename std::map<K,V>::const_iterator it = m.find( key );
if ( it == m.end() ) {
return defval;
}
else {
return it->second;
}
}
template<typename T>
void print_vector(const vector<T> &v) {
cerr << " { ";
for (typename vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
cerr << *it << ", ";
}
cerr << " } " << endl;
}
class AntsMeet {
/*
*/
public:
int countAnts(vector<int> x, vector<int> y, string direction) {
int n = x.size();
vector<vector<int> > points(n, vector<int>(2, 0));
vector<int> dir(n, 0);
typedef pair<int, pii> piii;
priority_queue<piii, vector<piii>, greater<piii> > pq;
unordered_map<char, int> m;
m['N'] = 1;
m['S'] = -1;
m['E'] = 2;
m['W'] = -2;
for (int i = 0; i < n; ++i) {
points[i][0] = x[i];
points[i][1] = y[i];
dir[i] = m[direction[i]];
}
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
// points travelling in the same direction cannot meet
if (dir[i] == dir[j]) {
continue;
}
if (abs(dir[i]) == abs(dir[j])) {
int abs_dir = abs(dir[i]);
if (points[i][abs_dir - 1] != points[j][abs_dir - 1]) {
continue;
}
if ((points[i][abs_dir % 2] * dir[i]) +
(points[j][abs_dir % 2] * dir[j]) < 0) {
pq.push(
make_pair(
abs(points[i][abs_dir % 2] - points[j][abs_dir % 2]),
make_pair(i, j)));
}
}
else {
// travelling horizontally, vertically
int h = i, v = j;
if (1 == abs(dir[i]))
swap(h, v);
int h_dist = points[v][0] - points[h][0];
int v_dist = points[h][1] - points[v][1];
// the horizontal
if (abs(h_dist) == abs(v_dist) &&
(h_dist * dir[h]) > 0 &&
(v_dist * dir[v]) > 0) {
pq.push(make_pair(abs(h_dist) * 2, make_pair(h, v)));
}
}
}
}
unordered_set<int> s;
for (int i = 0; i < n; ++i)
s.insert(i);
while (!pq.empty()) {
pair<int, pii> t = pq.top();
pq.pop();
if (s.find(t.second.first) != s.end() &&
s.find(t.second.second) != s.end()) {
unordered_set<int> s_temp;
/*
cout << "removing" << endl;
print_vector(points[t.second.first]);
print_vector(points[t.second.second]);
*/
s_temp.insert(t.second.first);
s_temp.insert(t.second.second);
while (!pq.empty()) {
pair<int, pii> p = pq.top();
if (p.first != t.first) {
break;
}
else {
pq.pop();
if (s.find(p.second.first) != s.end() &&
s.find(p.second.second) != s.end()) {
/*
cout << "removing2" << endl;
print_vector(points[p.second.first]);
print_vector(points[p.second.second]);
*/
s_temp.insert(p.second.first);
s_temp.insert(p.second.second);
}
}
}
for (unordered_set<int>::iterator it = s_temp.begin();
it != s_temp.end();
++it)
s.erase(*it);
}
}
return s.size();
}
bool runTest(vector<int> x, vector<int> y, string direction, int solution) {
int output = countAnts(x, y, direction);
if (output != solution)
{
cerr << "Failed for " << endl;
print_vector(x);
print_vector(y);
cout << direction << endl;
cerr << "Expected: " << solution << endl;
cerr << "Found instead: " << output << endl;
return false;
}
else {
return true;
}
}
};
int main(int argc, char* argv[]) {
AntsMeet test;
{
test.runTest({0,10,20,30}, {0,10,20,30}, "NWNE", 2);
test.runTest({-10,0,0,10}, {0,-10,10,0}, "NEWS", 0);
test.runTest({-1,-1,-1,0,0,0,1,1,1}, {-1,0,1,-1,0,1,-1,0,1}, "ESEWNNEWW", 4);
test.runTest({4,7,6,2,6,5,7,7,8,4,7,8,8,8,5,4,8,9,1,5,9,3,4,0,0,1,0,7,2,6,9,6,3,0,5,5,1,2,0,4,9,7,7,1,8,1,9,2,7,3}, {2,3,0,6,8,4,9,0,5,0,2,4,3,8,1,5,0,7,3,7,0,9,8,1,9,4,7,8,1,1,6,6,6,2,8,5,1,9,0,1,1,1,7,0,2,5,4,7,5,3}, "SSNWSWSENSWSESWEWSWSENWNNNESWSWSWWSSWEEWWNWWWNWENN", 25);
test.runTest({478,-664,759,434,-405,513,565,-396,311,-174,56,993,251,-341,993,-112,242,129,383,513,-78,-341,-148,129,423
,493,434,-405,478,-148,929,251,56,242,929,-78,423,-664,802,251,759,383,-112,-591,-591,-248,660,660,735,493}, {-186,98,948,795,289,-678,948,-170,-195,290,-354,-424,289,-157,-166,150,706,-678,684,-294,-234,36,36,-294,-216
,-234,427,945,265,-157,265,715,275,715,-186,337,798,-170,427,706,754,961,286,-216,798,286,961,684,-424,337}, "WNSNNSSWWWEENWESNSWSWSEWWEWEWWWNWESNSSNNSNNWWWNESE", 44);
}
}