-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwormhole.cpp
More file actions
82 lines (69 loc) · 1.62 KB
/
Copy pathwormhole.cpp
File metadata and controls
82 lines (69 loc) · 1.62 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
/*
ID: th3c0r11
TASK: wormhole
LANG: C++14
*/
/* entry and exit hole needed, so every included hole has another hole at the same height.
* */
#include <bits/stdc++.h>
int n;
std::vector<std::pair<long long, long long>> coords;
std::vector<int> partners;
int iterate()
{
int first = 0;
while (partners[first] != -1 && first != coords.size())
++first;
if (first == coords.size()) {
// check if cyclic
for (int start = 0; start < partners.size(); ++start) {
int curr = start;
for (int i = 0; i < n; ++i) {
long long minX = std::numeric_limits<long long>::max();
int next = -1;
for (int j = 0; j < coords.size(); ++j) {
if (coords[j].second == coords[partners[curr]].second && coords[j].first > coords[partners[curr]].first && coords[j].first < minX) {
next = j;
minX = coords[j].first;
}
}
if (next == -1)
break;
curr = next;
if (curr == start)
return 1;
}
}
for (int i = 0; i < partners.size(); ++i) {
std::cout << '(' << i << ',' << partners[i] << ");";
}
std::cout << '\n';
return 0;
}
int matches = 0;
for (int i = first + 1; i < coords.size(); ++i) {
if (partners[i] == -1) {
partners[first] = i;
partners[i] = first;
matches += iterate();
partners[i] = -1;
partners[first] = -1;
}
}
return matches;
}
int main()
{
std::ifstream in{"wormhole.in"};
std::ofstream out{"wormhole.out"};
in >> n;
in.ignore(100, '\n');
for (int i = 0; i < n; ++i) {
std::pair<long long, long long> pair;
in >> pair.first >> pair.second;
in.ignore(100, '\n');
coords.push_back(pair);
}
partners.resize(n, -1);
out << iterate() << '\n';
}