Skip to content

Commit 61b2251

Browse files
committed
AntsMeet.cpp
1 parent 10eb0fb commit 61b2251

File tree

1 file changed

+213
-0
lines changed

1 file changed

+213
-0
lines changed

AntsMeet.cpp

+213
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
#include <iostream>
2+
#include <iterator>
3+
#include <algorithm>
4+
#include <map>
5+
#include <vector>
6+
#include <iomanip>
7+
#include <sstream>
8+
#include <queue>
9+
#include <string>
10+
#include <cmath>
11+
#include <set>
12+
#include <limits>
13+
#include <numeric>
14+
#include <bitset>
15+
#include <stack>
16+
#include <utility>
17+
#include <unordered_set>
18+
#include <unordered_map>
19+
20+
#define DEBUG 0
21+
#define ARR_SIZE(x) ((sizeof(x)) / (sizeof(x[0])))
22+
#define INF_INT numeric_limits<int>::max()
23+
#define MIN_INT numeric_limits<int>::min()
24+
25+
using namespace std;
26+
27+
typedef unsigned long long ULL;
28+
typedef long long LL;
29+
typedef pair<int, int> pii;
30+
31+
vector<string> &split(const string &s, char delim, vector<string> &elems) {
32+
stringstream ss(s);
33+
string item;
34+
while (getline(ss, item, delim)) {
35+
elems.push_back(item);
36+
}
37+
return elems;
38+
}
39+
40+
vector<string> split(const string &s, char delim) {
41+
vector<string> elems;
42+
split(s, delim, elems);
43+
return elems;
44+
}
45+
46+
template <typename K, typename V>
47+
V GetWithDef(const std::map <K,V> & m, const K & key, const V & defval ) {
48+
typename std::map<K,V>::const_iterator it = m.find( key );
49+
if ( it == m.end() ) {
50+
return defval;
51+
}
52+
else {
53+
return it->second;
54+
}
55+
}
56+
57+
template<typename T>
58+
void print_vector(const vector<T> &v) {
59+
cerr << " { ";
60+
for (typename vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
61+
cerr << *it << ", ";
62+
}
63+
cerr << " } " << endl;
64+
}
65+
66+
class AntsMeet {
67+
/*
68+
*/
69+
70+
public:
71+
int countAnts(vector<int> x, vector<int> y, string direction) {
72+
int n = x.size();
73+
vector<vector<int> > points(n, vector<int>(2, 0));
74+
vector<int> dir(n, 0);
75+
typedef pair<int, pii> piii;
76+
priority_queue<piii, vector<piii>, greater<piii> > pq;
77+
unordered_map<char, int> m;
78+
79+
m['N'] = 1;
80+
m['S'] = -1;
81+
m['E'] = 2;
82+
m['W'] = -2;
83+
84+
for (int i = 0; i < n; ++i) {
85+
points[i][0] = x[i];
86+
points[i][1] = y[i];
87+
dir[i] = m[direction[i]];
88+
}
89+
90+
for (int i = 0; i < n; ++i) {
91+
for (int j = i + 1; j < n; ++j) {
92+
// points travelling in the same direction cannot meet
93+
if (dir[i] == dir[j]) {
94+
continue;
95+
}
96+
97+
if (abs(dir[i]) == abs(dir[j])) {
98+
int abs_dir = abs(dir[i]);
99+
if (points[i][abs_dir - 1] != points[j][abs_dir - 1]) {
100+
continue;
101+
}
102+
103+
if ((points[i][abs_dir % 2] * dir[i]) +
104+
(points[j][abs_dir % 2] * dir[j]) < 0) {
105+
pq.push(
106+
make_pair(
107+
abs(points[i][abs_dir % 2] - points[j][abs_dir % 2]),
108+
make_pair(i, j)));
109+
}
110+
}
111+
else {
112+
// travelling horizontally, vertically
113+
int h = i, v = j;
114+
115+
if (1 == abs(dir[i]))
116+
swap(h, v);
117+
118+
int h_dist = points[v][0] - points[h][0];
119+
int v_dist = points[h][1] - points[v][1];
120+
121+
// the horizontal
122+
if (abs(h_dist) == abs(v_dist) &&
123+
(h_dist * dir[h]) > 0 &&
124+
(v_dist * dir[v]) > 0) {
125+
pq.push(make_pair(abs(h_dist) * 2, make_pair(h, v)));
126+
}
127+
}
128+
}
129+
}
130+
131+
unordered_set<int> s;
132+
for (int i = 0; i < n; ++i)
133+
s.insert(i);
134+
135+
while (!pq.empty()) {
136+
pair<int, pii> t = pq.top();
137+
pq.pop();
138+
139+
if (s.find(t.second.first) != s.end() &&
140+
s.find(t.second.second) != s.end()) {
141+
142+
unordered_set<int> s_temp;
143+
144+
/*
145+
cout << "removing" << endl;
146+
print_vector(points[t.second.first]);
147+
print_vector(points[t.second.second]);
148+
*/
149+
150+
s_temp.insert(t.second.first);
151+
s_temp.insert(t.second.second);
152+
while (!pq.empty()) {
153+
pair<int, pii> p = pq.top();
154+
if (p.first != t.first) {
155+
break;
156+
}
157+
else {
158+
pq.pop();
159+
if (s.find(p.second.first) != s.end() &&
160+
s.find(p.second.second) != s.end()) {
161+
/*
162+
cout << "removing2" << endl;
163+
print_vector(points[p.second.first]);
164+
print_vector(points[p.second.second]);
165+
*/
166+
167+
s_temp.insert(p.second.first);
168+
s_temp.insert(p.second.second);
169+
}
170+
}
171+
}
172+
173+
for (unordered_set<int>::iterator it = s_temp.begin();
174+
it != s_temp.end();
175+
++it)
176+
s.erase(*it);
177+
}
178+
}
179+
return s.size();
180+
}
181+
182+
bool runTest(vector<int> x, vector<int> y, string direction, int solution) {
183+
int output = countAnts(x, y, direction);
184+
185+
if (output != solution)
186+
{
187+
cerr << "Failed for " << endl;
188+
print_vector(x);
189+
print_vector(y);
190+
cout << direction << endl;
191+
cerr << "Expected: " << solution << endl;
192+
cerr << "Found instead: " << output << endl;
193+
return false;
194+
}
195+
else {
196+
return true;
197+
}
198+
}
199+
};
200+
201+
int main(int argc, char* argv[]) {
202+
AntsMeet test;
203+
{
204+
test.runTest({0,10,20,30}, {0,10,20,30}, "NWNE", 2);
205+
test.runTest({-10,0,0,10}, {0,-10,10,0}, "NEWS", 0);
206+
test.runTest({-1,-1,-1,0,0,0,1,1,1}, {-1,0,1,-1,0,1,-1,0,1}, "ESEWNNEWW", 4);
207+
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);
208+
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
209+
,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
210+
,-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);
211+
}
212+
}
213+

0 commit comments

Comments
 (0)