#include <bits/stdc++.h>
using namespace std;
void solve() {
int a, b, c, A, B, C;
cin >> A >> B >> C >> a >> b >> c;
vector<int> res(C + 1, -1);
res[a] = 0;
res[b] = 0;
res[c] = 0;
set<tuple<int, int, int>> used;
used.insert({a, b, c});
set<tuple<int, int, int>> prev, next;
unordered_set<int> caught;
caught.insert(a);
caught.insert(b);
caught.insert(c);
prev.insert({a, b, c});
int steps = 1, amount;
tuple<int, int, int> tmp;
while (caught.size() < C + 1 && !prev.empty()) {
for (const auto & el : prev) {
for (int i = 0; i < 6; i++) {
if (i == 0) { // 1 -> 2
amount = min(get<0>(el), max(0, B - get<1>(el)));
tmp = {get<0>(el) - amount, get<1>(el) + amount, get<2>(el)};
} else if (i == 1) { // 1 -> 3
amount = min(get<0>(el), max(0, C - get<2>(el)));
tmp = {get<0>(el) - amount, get<1>(el), get<2>(el) + amount};
} else if (i == 2) { // 2 -> 1
amount = min(get<1>(el), max(0, A - get<0>(el)));
tmp = {get<0>(el) + amount, get<1>(el) - amount, get<2>(el)};
} else if (i == 3) { // 2 -> 3
amount = min(get<1>(el), max(0, C - get<2>(el)));
tmp = {get<0>(el), get<1>(el) - amount, get<2>(el) + amount};
} else if (i == 4) { // 3 -> 1
amount = min(get<2>(el), max(0, A - get<0>(el)));
tmp = {get<0>(el) + amount, get<1>(el), get<2>(el) - amount};
} else { // 3 -> 2
amount = min(get<2>(el), max(0, B - get<1>(el)));
tmp = {get<0>(el), get<1>(el) + amount, get<2>(el) - amount};
}
if (used.find(tmp) == used.end()) {
next.insert(tmp);
used.insert(tmp);
caught.insert(get<0>(tmp));
caught.insert(get<1>(tmp));
caught.insert(get<2>(tmp));
if (res[get<0>(tmp)] == -1)
res[get<0>(tmp)] = steps;
if (res[get<1>(tmp)] == -1)
res[get<1>(tmp)] = steps;
if (res[get<2>(tmp)] == -1)
res[get<2>(tmp)] = steps;
}
}
}
prev = std::move(next);
next = set<tuple<int, int, int>>();
steps++;
}
for (int i = 0; i < res.size(); i++) {
cout << res[i];
if (i < C)
cout << " ";
}
cout << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
}
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 | #include <bits/stdc++.h> using namespace std; void solve() { int a, b, c, A, B, C; cin >> A >> B >> C >> a >> b >> c; vector<int> res(C + 1, -1); res[a] = 0; res[b] = 0; res[c] = 0; set<tuple<int, int, int>> used; used.insert({a, b, c}); set<tuple<int, int, int>> prev, next; unordered_set<int> caught; caught.insert(a); caught.insert(b); caught.insert(c); prev.insert({a, b, c}); int steps = 1, amount; tuple<int, int, int> tmp; while (caught.size() < C + 1 && !prev.empty()) { for (const auto & el : prev) { for (int i = 0; i < 6; i++) { if (i == 0) { // 1 -> 2 amount = min(get<0>(el), max(0, B - get<1>(el))); tmp = {get<0>(el) - amount, get<1>(el) + amount, get<2>(el)}; } else if (i == 1) { // 1 -> 3 amount = min(get<0>(el), max(0, C - get<2>(el))); tmp = {get<0>(el) - amount, get<1>(el), get<2>(el) + amount}; } else if (i == 2) { // 2 -> 1 amount = min(get<1>(el), max(0, A - get<0>(el))); tmp = {get<0>(el) + amount, get<1>(el) - amount, get<2>(el)}; } else if (i == 3) { // 2 -> 3 amount = min(get<1>(el), max(0, C - get<2>(el))); tmp = {get<0>(el), get<1>(el) - amount, get<2>(el) + amount}; } else if (i == 4) { // 3 -> 1 amount = min(get<2>(el), max(0, A - get<0>(el))); tmp = {get<0>(el) + amount, get<1>(el), get<2>(el) - amount}; } else { // 3 -> 2 amount = min(get<2>(el), max(0, B - get<1>(el))); tmp = {get<0>(el), get<1>(el) + amount, get<2>(el) - amount}; } if (used.find(tmp) == used.end()) { next.insert(tmp); used.insert(tmp); caught.insert(get<0>(tmp)); caught.insert(get<1>(tmp)); caught.insert(get<2>(tmp)); if (res[get<0>(tmp)] == -1) res[get<0>(tmp)] = steps; if (res[get<1>(tmp)] == -1) res[get<1>(tmp)] = steps; if (res[get<2>(tmp)] == -1) res[get<2>(tmp)] = steps; } } } prev = std::move(next); next = set<tuple<int, int, int>>(); steps++; } for (int i = 0; i < res.size(); i++) { cout << res[i]; if (i < C) cout << " "; } cout << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); solve(); } |
English