#include <cstdio> #include <queue> #include <set> #include <cstdint> #include <tuple> constexpr int N = 100 * 1000 + 1; using state = std::tuple<int32_t, int32_t, int32_t>; std::pair<int32_t, int32_t> move(int32_t a, int32_t A, int32_t b, int32_t B) { if (a + b < B) { return {0, a + b}; } return {a - (B - b), B}; } uint32_t A, B, C; uint32_t results[N]; std::set<state> visited; int main() { uint32_t a, b, c; scanf("%u%u%u%u%u%u", &A, &B, &C, &a, &b, &c); std::queue<std::pair<state, uint32_t>> q; q.emplace(state(a, b, c), 0); visited.emplace(a, b, c); for (uint32_t i = 0; i <= C; ++i) { results[i] = -1; } results[a] = 0; results[b] = 0; results[c] = 0; while (!q.empty()) { state current_state; uint32_t current_res; std::tie(current_state, current_res) = q.front(); std::tie(a, b, c) = current_state; q.pop(); uint32_t s1, s2; std::tie(s1, s2) = move(a, A, b, B); results[s1] = std::min(results[s1], current_res + 1); results[s2] = std::min(results[s2], current_res + 1); if (!visited.count({s1, s2, c})) { visited.emplace(s1, s2, c); q.emplace(state(s1, s2, c), current_res + 1); } std::tie(s1, s2) = move(b, B, a, A); results[s1] = std::min(results[s1], current_res + 1); results[s2] = std::min(results[s2], current_res + 1); if (!visited.count({s2, s1, c})) { visited.emplace(s2, s1, c); q.emplace(state(s2, s1, c), current_res + 1); } std::tie(s1, s2) = move(a, A, c, C); results[s1] = std::min(results[s1], current_res + 1); results[s2] = std::min(results[s2], current_res + 1); if (!visited.count({s1, b, s2})) { visited.emplace(s1, b, s2); q.emplace(state(s1, b, s2), current_res + 1); } std::tie(s1, s2) = move(c, C, a, A); results[s1] = std::min(results[s1], current_res + 1); results[s2] = std::min(results[s2], current_res + 1); if (!visited.count({s2, b, s1})) { visited.emplace(s2, b, s1); q.emplace(state(s2, b, s1), current_res + 1); } std::tie(s1, s2) = move(b, B, c, C); results[s1] = std::min(results[s1], current_res + 1); results[s2] = std::min(results[s2], current_res + 1); if (!visited.count({a, s1, s2})) { visited.emplace(a, s1, s2); q.emplace(state(a, s1, s2), current_res + 1); } std::tie(s1, s2) = move(c, C, b, B); results[s1] = std::min(results[s1], current_res + 1); results[s2] = std::min(results[s2], current_res + 1); if (!visited.count({a, s2, s1})) { visited.emplace(a, s2, s1); q.emplace(state(a, s2, s1), current_res + 1); } } for (uint32_t i = 0; i <= C; ++i) { printf("%d ", (int32_t)(results[i])); } 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 | #include <cstdio> #include <queue> #include <set> #include <cstdint> #include <tuple> constexpr int N = 100 * 1000 + 1; using state = std::tuple<int32_t, int32_t, int32_t>; std::pair<int32_t, int32_t> move(int32_t a, int32_t A, int32_t b, int32_t B) { if (a + b < B) { return {0, a + b}; } return {a - (B - b), B}; } uint32_t A, B, C; uint32_t results[N]; std::set<state> visited; int main() { uint32_t a, b, c; scanf("%u%u%u%u%u%u", &A, &B, &C, &a, &b, &c); std::queue<std::pair<state, uint32_t>> q; q.emplace(state(a, b, c), 0); visited.emplace(a, b, c); for (uint32_t i = 0; i <= C; ++i) { results[i] = -1; } results[a] = 0; results[b] = 0; results[c] = 0; while (!q.empty()) { state current_state; uint32_t current_res; std::tie(current_state, current_res) = q.front(); std::tie(a, b, c) = current_state; q.pop(); uint32_t s1, s2; std::tie(s1, s2) = move(a, A, b, B); results[s1] = std::min(results[s1], current_res + 1); results[s2] = std::min(results[s2], current_res + 1); if (!visited.count({s1, s2, c})) { visited.emplace(s1, s2, c); q.emplace(state(s1, s2, c), current_res + 1); } std::tie(s1, s2) = move(b, B, a, A); results[s1] = std::min(results[s1], current_res + 1); results[s2] = std::min(results[s2], current_res + 1); if (!visited.count({s2, s1, c})) { visited.emplace(s2, s1, c); q.emplace(state(s2, s1, c), current_res + 1); } std::tie(s1, s2) = move(a, A, c, C); results[s1] = std::min(results[s1], current_res + 1); results[s2] = std::min(results[s2], current_res + 1); if (!visited.count({s1, b, s2})) { visited.emplace(s1, b, s2); q.emplace(state(s1, b, s2), current_res + 1); } std::tie(s1, s2) = move(c, C, a, A); results[s1] = std::min(results[s1], current_res + 1); results[s2] = std::min(results[s2], current_res + 1); if (!visited.count({s2, b, s1})) { visited.emplace(s2, b, s1); q.emplace(state(s2, b, s1), current_res + 1); } std::tie(s1, s2) = move(b, B, c, C); results[s1] = std::min(results[s1], current_res + 1); results[s2] = std::min(results[s2], current_res + 1); if (!visited.count({a, s1, s2})) { visited.emplace(a, s1, s2); q.emplace(state(a, s1, s2), current_res + 1); } std::tie(s1, s2) = move(c, C, b, B); results[s1] = std::min(results[s1], current_res + 1); results[s2] = std::min(results[s2], current_res + 1); if (!visited.count({a, s2, s1})) { visited.emplace(a, s2, s1); q.emplace(state(a, s2, s1), current_res + 1); } } for (uint32_t i = 0; i <= C; ++i) { printf("%d ", (int32_t)(results[i])); } return 0; } |