#include <cstdio> #include <algorithm> #include <queue> #include <functional> #include <unordered_set> using namespace std; typedef pair<pair<int, int>, int> S; template <> struct std::hash<S> { std::size_t operator()(const S& s) const noexcept { const auto& [ab,c] = s; const auto& [a,b] = ab; return std::hash<int>()(a) ^ (hash<int>()(b) << 1) ^ (hash<int>()(c) << 2); } }; const int INF = 1000000000; inline S getS(int a, int b, int c) { if (a < 0 || b < 0 || c < 0) exit(1); // printf("S - a:%d, b:%d, c:%d\n", a, b, c); return S{{a,b},c}; } int left(int x, int y, int X, int Y) { // printf("left: %d %d %d %d\n", x, y, X, Y); return min(X-x, y); } int right(int x, int y, int X, int Y) { // printf("right: %d %d %d %d\n", x, y, X, Y); return min(x, Y-y); } int main() { int A, B, C; scanf("%d %d %d", &A, &B, &C); int a, b, c; scanf("%d %d %d", &a, &b, &c); vector<int> res(C+1, INF); unordered_set<S> vis; vector<pair<S, int>> list; int p = 0; list.push_back({getS(a, b, c), 0}); while (p < list.size()) { const auto [s, d] = list[p++]; const auto& [ab,c] = s; const auto& [a,b] = ab; if (!vis.insert(s).second) continue; res[a] = min(res[a], d); res[b] = min(res[b], d); res[c] = min(res[c], d); // printf("%d\n", d); // printf("a:%d, b:%d, c:%d\n", a, b, c); int la = left(a, b, A, B); if (la > 0) list.push_back({getS(a + la, b - la, c), d+1}); int lb = left(b, c, B, C); if (lb > 0) list.push_back({getS(a, b + lb, c - lb), d+1}); int lc = left(c, a, C, A); if (lc > 0) list.push_back({getS(a - lc, b, c + lc), d+1}); int ra = right(a, b, A, B); if (ra > 0) list.push_back({getS(a - ra, b + ra, c), d+1}); int rb = right(b, c, B, C); if (rb > 0) list.push_back({getS(a, b - rb, c + rb), d+1}); int rc = right(c, a, C, A); if (rc > 0) list.push_back({getS(a + rc, b, c - rc), d+1}); } for (int i=0; i<C+1; ++i) { if (res[i] == INF) { printf("-1 "); } else { printf("%d ", res[i]); } } printf("\n"); return 0; }
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 83 84 | #include <cstdio> #include <algorithm> #include <queue> #include <functional> #include <unordered_set> using namespace std; typedef pair<pair<int, int>, int> S; template <> struct std::hash<S> { std::size_t operator()(const S& s) const noexcept { const auto& [ab,c] = s; const auto& [a,b] = ab; return std::hash<int>()(a) ^ (hash<int>()(b) << 1) ^ (hash<int>()(c) << 2); } }; const int INF = 1000000000; inline S getS(int a, int b, int c) { if (a < 0 || b < 0 || c < 0) exit(1); // printf("S - a:%d, b:%d, c:%d\n", a, b, c); return S{{a,b},c}; } int left(int x, int y, int X, int Y) { // printf("left: %d %d %d %d\n", x, y, X, Y); return min(X-x, y); } int right(int x, int y, int X, int Y) { // printf("right: %d %d %d %d\n", x, y, X, Y); return min(x, Y-y); } int main() { int A, B, C; scanf("%d %d %d", &A, &B, &C); int a, b, c; scanf("%d %d %d", &a, &b, &c); vector<int> res(C+1, INF); unordered_set<S> vis; vector<pair<S, int>> list; int p = 0; list.push_back({getS(a, b, c), 0}); while (p < list.size()) { const auto [s, d] = list[p++]; const auto& [ab,c] = s; const auto& [a,b] = ab; if (!vis.insert(s).second) continue; res[a] = min(res[a], d); res[b] = min(res[b], d); res[c] = min(res[c], d); // printf("%d\n", d); // printf("a:%d, b:%d, c:%d\n", a, b, c); int la = left(a, b, A, B); if (la > 0) list.push_back({getS(a + la, b - la, c), d+1}); int lb = left(b, c, B, C); if (lb > 0) list.push_back({getS(a, b + lb, c - lb), d+1}); int lc = left(c, a, C, A); if (lc > 0) list.push_back({getS(a - lc, b, c + lc), d+1}); int ra = right(a, b, A, B); if (ra > 0) list.push_back({getS(a - ra, b + ra, c), d+1}); int rb = right(b, c, B, C); if (rb > 0) list.push_back({getS(a, b - rb, c + rb), d+1}); int rc = right(c, a, C, A); if (rc > 0) list.push_back({getS(a + rc, b, c - rc), d+1}); } for (int i=0; i<C+1; ++i) { if (res[i] == INF) { printf("-1 "); } else { printf("%d ", res[i]); } } printf("\n"); return 0; } |