-
Notifications
You must be signed in to change notification settings - Fork 0
/
68.cpp
62 lines (55 loc) · 1.28 KB
/
68.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
#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>
// not sure why 6549438327211015 is incorrect
// brute force search
std::string concat(std::vector<int> &v) {
int min = INT_MAX;
int offset = 0;
for (int i = 0; i < 5; ++i) {
if (v[i] < min) {
min = v[i];
offset = i;
}
}
std::string ans;
int sum = 0;
for (int i = 0; i < 5; ++i) {
int idx1 = (i + offset) % 5;
int idx2 = idx1 + 5;
int idx3 = (idx1 + 6) % 5 + 5;
int local_sum = v[idx1] + v[idx2] + v[idx3];
if (sum == 0) sum = local_sum;
else if (local_sum != sum) return "0";
ans += std::to_string(v[idx1]);
ans += std::to_string(v[idx2]);
ans += std::to_string(v[idx3]);
}
return ans;
}
void dfs(std::vector<int> &v, std::unordered_set<int> unused, std::string &best_ans) {
if (unused.empty()) {
best_ans = std::max(best_ans, concat(v));
return;
}
if (unused.size() == 5 && (unused.find(10) != unused.end())) {
return;
}
auto copy = unused;
for (auto &x : copy) {
v.push_back(x);
unused.erase(x);
dfs(v, unused, best_ans);
unused.insert(x);
v.pop_back();
}
}
int main() {
std::vector<int> v;
std::unordered_set<int> unused;
for (int i = 1; i <= 10; ++i) unused.insert(i);
std::string ans = "0";
dfs(v, unused, ans);
std::cout << ans << std::endl;
}