#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; typedef long long ll; typedef long double ld; typedef pair<int, int> PII; typedef pair<ll, int> PILL; typedef pair<ll, ll> PLL; const int MAX_N = 1e5+5; const int M = 3e4+5; const ll INF = (ll)(1e18); const int inf = 1e9; const ll MOD = 1000000007LL; int A, B, C; set<pair<PII, int>> cnt; int ans[MAX_N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> A >> B >> C; int a, b, c; cin >> a >> b >> c; for (int i = 0; i <= C; i++) { ans[i] = inf; } queue<pair<PII, PII>> Q; pair<PII, PII> tmp = make_pair(make_pair(a, b), make_pair(c, 0)); Q.push(tmp); while (!Q.empty()) { tmp = Q.front(); Q.pop(); a = tmp.first.first; b = tmp.first.second; c = tmp.second.first; int steps = tmp.second.second; /*cout << "STEPS: " << steps << '\n'; cout << "A: " << a << '\n'; cout << "B: " << b << '\n'; cout << "C: " << c << '\n';*/ ans[a] = min(ans[a], steps); ans[b] = min(ans[b], steps); ans[c] = min(ans[c], steps); // 1 -> 2 if (a > 0) { int x = min(a, B - b); if (x > 0) { pair<PII, int> state = make_pair(make_pair(a - x, b + x), c); if (!cnt.count(state)) { Q.push(make_pair(make_pair(a - x, b + x), make_pair(c, steps + 1))); cnt.insert(state); } } } // 1 -> 3 if (a > 0) { int x = min(a, C - c); if (x > 0) { pair<PII, int> state = make_pair(make_pair(a - x, b), c + x); if (!cnt.count(state)) { Q.push(make_pair(make_pair(a - x, b), make_pair(c + x, steps + 1))); cnt.insert(state); } } } // 2 -> 3 if (b > 0) { int x = min(b, C - c); if (x > 0) { pair<PII, int> state = make_pair(make_pair(a, b - x), c + x); if (!cnt.count(state)) { Q.push(make_pair(make_pair(a, b - x), make_pair(c + x, steps + 1))); cnt.insert(state); } } } // 2 -> 1 if (b > 0) { int x = min(b, A - a); if (x > 0) { pair<PII, int> state = make_pair(make_pair(a + x, b - x), c); if (!cnt.count(state)) { Q.push(make_pair(make_pair(a + x, b - x), make_pair(c, steps + 1))); cnt.insert(state); } } } // 3 -> 1 if (c > 0) { int x = min(c, A - a); if (x > 0) { pair<PII, int> state = make_pair(make_pair(a + x, b), c - x); if (!cnt.count(state)) { Q.push(make_pair(make_pair(a + x, b), make_pair(c - x, steps + 1))); cnt.insert(state); } } } // 3 -> 2 if (c > 0) { int x = min(c, B - b); if (x > 0) { pair<PII, int> state = make_pair(make_pair(a, b + x), c - x); if (!cnt.count(state)) { Q.push(make_pair(make_pair(a, b + x), make_pair(c - x, steps + 1))); cnt.insert(state); } } } } for (int i = 0; i <= C; i++) { if (ans[i] == inf) { cout << -1; } else { cout << ans[i]; } cout << ' '; } cout << '\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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | #include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; typedef long long ll; typedef long double ld; typedef pair<int, int> PII; typedef pair<ll, int> PILL; typedef pair<ll, ll> PLL; const int MAX_N = 1e5+5; const int M = 3e4+5; const ll INF = (ll)(1e18); const int inf = 1e9; const ll MOD = 1000000007LL; int A, B, C; set<pair<PII, int>> cnt; int ans[MAX_N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> A >> B >> C; int a, b, c; cin >> a >> b >> c; for (int i = 0; i <= C; i++) { ans[i] = inf; } queue<pair<PII, PII>> Q; pair<PII, PII> tmp = make_pair(make_pair(a, b), make_pair(c, 0)); Q.push(tmp); while (!Q.empty()) { tmp = Q.front(); Q.pop(); a = tmp.first.first; b = tmp.first.second; c = tmp.second.first; int steps = tmp.second.second; /*cout << "STEPS: " << steps << '\n'; cout << "A: " << a << '\n'; cout << "B: " << b << '\n'; cout << "C: " << c << '\n';*/ ans[a] = min(ans[a], steps); ans[b] = min(ans[b], steps); ans[c] = min(ans[c], steps); // 1 -> 2 if (a > 0) { int x = min(a, B - b); if (x > 0) { pair<PII, int> state = make_pair(make_pair(a - x, b + x), c); if (!cnt.count(state)) { Q.push(make_pair(make_pair(a - x, b + x), make_pair(c, steps + 1))); cnt.insert(state); } } } // 1 -> 3 if (a > 0) { int x = min(a, C - c); if (x > 0) { pair<PII, int> state = make_pair(make_pair(a - x, b), c + x); if (!cnt.count(state)) { Q.push(make_pair(make_pair(a - x, b), make_pair(c + x, steps + 1))); cnt.insert(state); } } } // 2 -> 3 if (b > 0) { int x = min(b, C - c); if (x > 0) { pair<PII, int> state = make_pair(make_pair(a, b - x), c + x); if (!cnt.count(state)) { Q.push(make_pair(make_pair(a, b - x), make_pair(c + x, steps + 1))); cnt.insert(state); } } } // 2 -> 1 if (b > 0) { int x = min(b, A - a); if (x > 0) { pair<PII, int> state = make_pair(make_pair(a + x, b - x), c); if (!cnt.count(state)) { Q.push(make_pair(make_pair(a + x, b - x), make_pair(c, steps + 1))); cnt.insert(state); } } } // 3 -> 1 if (c > 0) { int x = min(c, A - a); if (x > 0) { pair<PII, int> state = make_pair(make_pair(a + x, b), c - x); if (!cnt.count(state)) { Q.push(make_pair(make_pair(a + x, b), make_pair(c - x, steps + 1))); cnt.insert(state); } } } // 3 -> 2 if (c > 0) { int x = min(c, B - b); if (x > 0) { pair<PII, int> state = make_pair(make_pair(a, b + x), c - x); if (!cnt.count(state)) { Q.push(make_pair(make_pair(a, b + x), make_pair(c - x, steps + 1))); cnt.insert(state); } } } } for (int i = 0; i <= C; i++) { if (ans[i] == inf) { cout << -1; } else { cout << ans[i]; } cout << ' '; } cout << '\n'; return 0; } |