#include <bits/stdc++.h> const int MAXA = 100'005; const int RNG = MAXA * 6; int res[MAXA]; int vis[RNG]; int A, B, C; int CAP[3]; struct trip { int t[3]; trip() {} trip(int a, int b, int c) { t[0] = a; t[1] = b; t[2] = c; } }; int trip_id(int a, int b, int c) { if (a == 0) return b; if (a == A) return MAXA + b; if (b == 0) return 2*MAXA + a; if (b == B) return 3*MAXA + a; if (c == 0) return 4*MAXA + a; if (c == C) return 5*MAXA + a; return RNG - 1; } trip move(trip x, int from, int to) { x.t[to] += x.t[from]; x.t[from] = std::max(0, x.t[to] - CAP[to]); x.t[to] = std::min(x.t[to], CAP[to]); return x; } int main() { scanf("%d%d%d", &A, &B, &C); CAP[0] = A; CAP[1] = B; CAP[2] = C; int a, b, c; scanf("%d%d%d", &a, &b, &c); std::queue<trip> q; q.push(trip(a, b, c)); vis[trip_id(a, b, c)] = 1; while (!q.empty()) { auto t = q.front(); q.pop(); int id = trip_id(t.t[0], t.t[1], t.t[2]); for (int i=0; i<3; i++) { if (res[t.t[i]] == 0) { res[t.t[i]] = vis[id]; } } for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { if (i == j) continue; // from i to j auto x = move(t, i, j); int x_id = trip_id(x.t[0], x.t[1], x.t[2]); if (vis[x_id]) continue; vis[x_id] = vis[id] + 1; q.push(x); } } } for (int i=0; i<=C; i++) printf("%d ", res[i] - 1); }
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 | #include <bits/stdc++.h> const int MAXA = 100'005; const int RNG = MAXA * 6; int res[MAXA]; int vis[RNG]; int A, B, C; int CAP[3]; struct trip { int t[3]; trip() {} trip(int a, int b, int c) { t[0] = a; t[1] = b; t[2] = c; } }; int trip_id(int a, int b, int c) { if (a == 0) return b; if (a == A) return MAXA + b; if (b == 0) return 2*MAXA + a; if (b == B) return 3*MAXA + a; if (c == 0) return 4*MAXA + a; if (c == C) return 5*MAXA + a; return RNG - 1; } trip move(trip x, int from, int to) { x.t[to] += x.t[from]; x.t[from] = std::max(0, x.t[to] - CAP[to]); x.t[to] = std::min(x.t[to], CAP[to]); return x; } int main() { scanf("%d%d%d", &A, &B, &C); CAP[0] = A; CAP[1] = B; CAP[2] = C; int a, b, c; scanf("%d%d%d", &a, &b, &c); std::queue<trip> q; q.push(trip(a, b, c)); vis[trip_id(a, b, c)] = 1; while (!q.empty()) { auto t = q.front(); q.pop(); int id = trip_id(t.t[0], t.t[1], t.t[2]); for (int i=0; i<3; i++) { if (res[t.t[i]] == 0) { res[t.t[i]] = vis[id]; } } for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { if (i == j) continue; // from i to j auto x = move(t, i, j); int x_id = trip_id(x.t[0], x.t[1], x.t[2]); if (vis[x_id]) continue; vis[x_id] = vis[id] + 1; q.push(x); } } } for (int i=0; i<=C; i++) printf("%d ", res[i] - 1); } |