#include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second const int N = 100100; const int inf = N*7; int A,B,C; enum StateType { A_ZERO, A_FULL, B_ZERO, B_FULL, C_ZERO, C_FULL, NONE }; struct State { int a,b,c; StateType type; int val; State(int a_=0, int b_=0, int c_=0) { a = a_; b = b_; c = c_; type = NONE; val = 0; if (a == 0) { type = A_ZERO; val = b; } else if (a == A) { type = A_FULL; val = b; } else if (b == 0) { type = B_ZERO; val = a; } else if (b == B) { type = B_FULL; val = a; } else if (c == 0) { type = C_ZERO; val = a; } else if (c == C) { type = C_FULL; val = a; } } }; int dst[6][N]; int res[N]; State nxt[6]; int main() { int a,b,c; scanf("%d%d%d%d%d%d", &A, &B, &C, &a, &b, &c); FOR(i,6) FOR(j,C+1) dst[i][j] = inf; FOR(j,C+1) res[j] = inf; queue<State> q; State start(a,b,c); start.type = NONE; q.push(start); while (!q.empty()) { State s = q.front(); q.pop(); a = s.a; b = s.b; c = s.c; int cur_dst = (s.type == NONE ? 0 : dst[s.type][s.val]); if (res[a] == inf) res[a] = cur_dst; if (res[b] == inf) res[b] = cur_dst; if (res[c] == inf) res[c] = cur_dst; int m = 0; /* a -> b */ m = min(a, B-b); nxt[0] = State(a-m, b+m, c ); /* a -> c */ m = min(a, C-c); nxt[1] = State(a-m, b , c+m); /* b -> a */ m = min(b, A-a); nxt[2] = State(a+m, b-m, c ); /* b -> c */ m = min(b, C-c); nxt[3] = State(a , b-m, c+m); /* c -> a */ m = min(c, A-a); nxt[4] = State(a+m, b , c-m); /* c -> b */ m = min(c, B-b); nxt[5] = State(a , b+m, c-m); FOR(i,6) { if (dst[nxt[i].type][nxt[i].val] == inf) { dst[nxt[i].type][nxt[i].val] = cur_dst + 1; q.push(nxt[i]); } } } FOR(i,C+1) printf("%d ", (res[i]==inf ? -1 : res[i])); printf("\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 | #include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second const int N = 100100; const int inf = N*7; int A,B,C; enum StateType { A_ZERO, A_FULL, B_ZERO, B_FULL, C_ZERO, C_FULL, NONE }; struct State { int a,b,c; StateType type; int val; State(int a_=0, int b_=0, int c_=0) { a = a_; b = b_; c = c_; type = NONE; val = 0; if (a == 0) { type = A_ZERO; val = b; } else if (a == A) { type = A_FULL; val = b; } else if (b == 0) { type = B_ZERO; val = a; } else if (b == B) { type = B_FULL; val = a; } else if (c == 0) { type = C_ZERO; val = a; } else if (c == C) { type = C_FULL; val = a; } } }; int dst[6][N]; int res[N]; State nxt[6]; int main() { int a,b,c; scanf("%d%d%d%d%d%d", &A, &B, &C, &a, &b, &c); FOR(i,6) FOR(j,C+1) dst[i][j] = inf; FOR(j,C+1) res[j] = inf; queue<State> q; State start(a,b,c); start.type = NONE; q.push(start); while (!q.empty()) { State s = q.front(); q.pop(); a = s.a; b = s.b; c = s.c; int cur_dst = (s.type == NONE ? 0 : dst[s.type][s.val]); if (res[a] == inf) res[a] = cur_dst; if (res[b] == inf) res[b] = cur_dst; if (res[c] == inf) res[c] = cur_dst; int m = 0; /* a -> b */ m = min(a, B-b); nxt[0] = State(a-m, b+m, c ); /* a -> c */ m = min(a, C-c); nxt[1] = State(a-m, b , c+m); /* b -> a */ m = min(b, A-a); nxt[2] = State(a+m, b-m, c ); /* b -> c */ m = min(b, C-c); nxt[3] = State(a , b-m, c+m); /* c -> a */ m = min(c, A-a); nxt[4] = State(a+m, b , c-m); /* c -> b */ m = min(c, B-b); nxt[5] = State(a , b+m, c-m); FOR(i,6) { if (dst[nxt[i].type][nxt[i].val] == inf) { dst[nxt[i].type][nxt[i].val] = cur_dst + 1; q.push(nxt[i]); } } } FOR(i,C+1) printf("%d ", (res[i]==inf ? -1 : res[i])); printf("\n"); return 0; } |