#include <bits/stdc++.h> using namespace std; int main() { cin.tie(nullptr)->sync_with_stdio(false); int A, B, C, a, b, c; cin >> A >> B >> C >> a >> b >> c; vector<int> ans(C + 1, 1e9); map<tuple<int, int, int>, int> d; queue<tuple<int, int, int>> Q; auto consider = [&](int x, int y, int z, int nw) { auto &state = d[{x, y, z}]; if (state == 0 && (x != a || y != b || z != c)) { Q.emplace(x, y, z); state = nw; } }; Q.emplace(a, b, c); d[{a, b, c}] = 0; while (!Q.empty()) { auto [n, i, g] = Q.front(); Q.pop(); int dx = d[{n, i, g}]; ans[n] = min(ans[n], dx); ans[i] = min(ans[i], dx); ans[g] = min(ans[g], dx); if (n > 0 && i < B) { int delta = min(B - i, n); consider(n - delta, i + delta, g, dx + 1); } if (n > 0 && g < C) { int delta = min(C - g, n); consider(n - delta, i, g + delta, dx + 1); } if (i > 0 && n < A) { int delta = min(A - n, i); consider(n + delta, i - delta, g, dx + 1); } if (i > 0 && g < C) { int delta = min(C - g, i); consider(n, i - delta, g + delta, dx + 1); } if (g > 0 && n < A) { int delta = min(A - n, g); consider(n + delta, i, g - delta, dx + 1); } if (g > 0 && i < B) { int delta = min(B - i, g); consider(n, i + delta, g - delta, dx + 1); } } for (auto i : ans) cout << (i == 1e9 ? -1 : i) << " "; cout << '\n'; }
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 | #include <bits/stdc++.h> using namespace std; int main() { cin.tie(nullptr)->sync_with_stdio(false); int A, B, C, a, b, c; cin >> A >> B >> C >> a >> b >> c; vector<int> ans(C + 1, 1e9); map<tuple<int, int, int>, int> d; queue<tuple<int, int, int>> Q; auto consider = [&](int x, int y, int z, int nw) { auto &state = d[{x, y, z}]; if (state == 0 && (x != a || y != b || z != c)) { Q.emplace(x, y, z); state = nw; } }; Q.emplace(a, b, c); d[{a, b, c}] = 0; while (!Q.empty()) { auto [n, i, g] = Q.front(); Q.pop(); int dx = d[{n, i, g}]; ans[n] = min(ans[n], dx); ans[i] = min(ans[i], dx); ans[g] = min(ans[g], dx); if (n > 0 && i < B) { int delta = min(B - i, n); consider(n - delta, i + delta, g, dx + 1); } if (n > 0 && g < C) { int delta = min(C - g, n); consider(n - delta, i, g + delta, dx + 1); } if (i > 0 && n < A) { int delta = min(A - n, i); consider(n + delta, i - delta, g, dx + 1); } if (i > 0 && g < C) { int delta = min(C - g, i); consider(n, i - delta, g + delta, dx + 1); } if (g > 0 && n < A) { int delta = min(A - n, g); consider(n + delta, i, g - delta, dx + 1); } if (g > 0 && i < B) { int delta = min(B - i, g); consider(n, i + delta, g - delta, dx + 1); } } for (auto i : ans) cout << (i == 1e9 ? -1 : i) << " "; cout << '\n'; } |