#include <bits/stdc++.h> using namespace std; int A, B, C, res[100002]; map <pair <int, pair <int, int> >, bool> mp; void bfs(int as, int bs, int cs) { queue <pair <pair <int, int>, pair <int, int> > > q; q.push({{as, bs}, {cs, 0}}); while(q.size()) { int a = q.front().first.first, b = q.front().first.second, c = q.front().second.first, d = q.front().second.second; q.pop(); int na, nb, nc; res[a] = min(res[a], d); res[b] = min(res[b], d); res[c] = min(res[c], d); auto go = [&]() { bool &tp = mp[{na, {nb, nc}}]; if(!tp) { tp = 1; q.push({{na, nb}, {nc, d + 1}}); } }; na = a - min(a, B - b); nb = b + min(a, B - b); nc = c; go(); na = a + min(b, A - a); nb = b - min(b, A - a); nc = c; go(); na = a - min(a, C - c); nb = b; nc = c + min(a, C - c); go(); na = a + min(c, A - a); nb = b; nc = c - min(c, A - a); go(); na = a; nb = b - min(b, C - c); nc = c + min(b, C - c); go(); na = a; nb = b + min(c, B - b); nc = c - min(c, B - b); go(); } } int main() { int a, b, c; scanf("%d%d%d%d%d%d", &A, &B, &C, &a, &b, &c); for(int i = 0; i <= C; i++) res[i] = INT_MAX; bfs(a, b, c); for(int i = 0; i <= C; i++) printf("%d ", res[i] == INT_MAX ? -1 : res[i]); }
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 | #include <bits/stdc++.h> using namespace std; int A, B, C, res[100002]; map <pair <int, pair <int, int> >, bool> mp; void bfs(int as, int bs, int cs) { queue <pair <pair <int, int>, pair <int, int> > > q; q.push({{as, bs}, {cs, 0}}); while(q.size()) { int a = q.front().first.first, b = q.front().first.second, c = q.front().second.first, d = q.front().second.second; q.pop(); int na, nb, nc; res[a] = min(res[a], d); res[b] = min(res[b], d); res[c] = min(res[c], d); auto go = [&]() { bool &tp = mp[{na, {nb, nc}}]; if(!tp) { tp = 1; q.push({{na, nb}, {nc, d + 1}}); } }; na = a - min(a, B - b); nb = b + min(a, B - b); nc = c; go(); na = a + min(b, A - a); nb = b - min(b, A - a); nc = c; go(); na = a - min(a, C - c); nb = b; nc = c + min(a, C - c); go(); na = a + min(c, A - a); nb = b; nc = c - min(c, A - a); go(); na = a; nb = b - min(b, C - c); nc = c + min(b, C - c); go(); na = a; nb = b + min(c, B - b); nc = c - min(c, B - b); go(); } } int main() { int a, b, c; scanf("%d%d%d%d%d%d", &A, &B, &C, &a, &b, &c); for(int i = 0; i <= C; i++) res[i] = INT_MAX; bfs(a, b, c); for(int i = 0; i <= C; i++) printf("%d ", res[i] == INT_MAX ? -1 : res[i]); } |