/* 2021 * Maciej Szeptuch */ #include <cstdio> #include <queue> #include <unordered_map> int A; int B; int C; int result[131072]; struct hash_pair { size_t operator()(const std::pair<int, int> &p) const { auto first = std::hash<int>{}(p.first); auto second = std::hash<int>{}(p.second); return first ^ (second + 0x9e3779b9 + (first << 6) + (first >> 2)); } }; std::unordered_map<std::pair<int, int>, int, hash_pair> steps; void solve(int a, int b, int total); int main(void) { int a; int b; int c; int total; scanf("%d %d %d %d %d %d", &A, &B, &C, &a, &b, &c); total = a + b + c; solve(a, b, total); for(int i = 0; i <= C; ++i) printf("%d ", result[i] - 1); puts(""); return 0; } void solve(int a, int b, int total) { std::queue<std::pair<int, int>> que; std::pair<int, int> pair; steps[{a, b}] = 1; que.push({a, b}); while(!que.empty()) { auto t = que.front(); a = t.first; b = t.second; int c = total - a - b; int _steps = steps[{a, b}]; que.pop(); if(!result[a]) result[a] = _steps; if(!result[b]) result[b] = _steps; if(!result[c]) result[c] = _steps; for(auto &next: std::vector<std::pair<int, int>>{ {a - std::min(a, B - b), b + std::min(a, B - b)}, // 1 -> 2 {a - std::min(a, C - c), b}, // 1 -> 3 {a + std::min(b, A - a), b - std::min(b, A - a)}, // 2 -> 1 {a + std::min(c, A - a), b}, // 3 - > 1 {a, b + std::min(c, B - b)}, // 3 -> 2 {a, b - std::min(b, C - c)}, // 2 -> 3 }) { if(!steps[next]) { steps[next] = _steps + 1; que.push(next); } } } }
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 | /* 2021 * Maciej Szeptuch */ #include <cstdio> #include <queue> #include <unordered_map> int A; int B; int C; int result[131072]; struct hash_pair { size_t operator()(const std::pair<int, int> &p) const { auto first = std::hash<int>{}(p.first); auto second = std::hash<int>{}(p.second); return first ^ (second + 0x9e3779b9 + (first << 6) + (first >> 2)); } }; std::unordered_map<std::pair<int, int>, int, hash_pair> steps; void solve(int a, int b, int total); int main(void) { int a; int b; int c; int total; scanf("%d %d %d %d %d %d", &A, &B, &C, &a, &b, &c); total = a + b + c; solve(a, b, total); for(int i = 0; i <= C; ++i) printf("%d ", result[i] - 1); puts(""); return 0; } void solve(int a, int b, int total) { std::queue<std::pair<int, int>> que; std::pair<int, int> pair; steps[{a, b}] = 1; que.push({a, b}); while(!que.empty()) { auto t = que.front(); a = t.first; b = t.second; int c = total - a - b; int _steps = steps[{a, b}]; que.pop(); if(!result[a]) result[a] = _steps; if(!result[b]) result[b] = _steps; if(!result[c]) result[c] = _steps; for(auto &next: std::vector<std::pair<int, int>>{ {a - std::min(a, B - b), b + std::min(a, B - b)}, // 1 -> 2 {a - std::min(a, C - c), b}, // 1 -> 3 {a + std::min(b, A - a), b - std::min(b, A - a)}, // 2 -> 1 {a + std::min(c, A - a), b}, // 3 - > 1 {a, b + std::min(c, B - b)}, // 3 -> 2 {a, b - std::min(b, C - c)}, // 2 -> 3 }) { if(!steps[next]) { steps[next] = _steps + 1; que.push(next); } } } } |