#include <cstdio> #include <vector> #include <set> using namespace std; #define FOR(i,a,b) for(int (i)=(int)(a); (i)!=(int)(b); ++(i)) int A, B, C; vector<int> result; struct State { int a, b, c; bool operator<(const State &other) const { if (a != other.a) return a < other.a; if (b != other.b) return b < other.b; return c < other.c; } }; void update_result(const State &state, int stage) { if (result[state.a] == -1) result[state.a] = stage; if (result[state.b] == -1) result[state.b] = stage; if (result[state.c] == -1) result[state.c] = stage; } void move(int &a, int &b, int max_b) { if (a+b > max_b) { a -= max_b - b; b = max_b; } else { b += a; a = 0; } } set<State> Q, used; int main() { scanf("%d %d %d", &A, &B, &C); result.resize(C+1, -1); State init; scanf("%d %d %d", &init.a, &init.b, &init.c); update_result(init, 0); used.insert(init); Q.insert(init); int stage = 0; while (Q.size() > 0) { ++stage; set<State> newStates; for(set<State>::iterator it = Q.begin(); it != Q.end(); ++it) { const State &state = *it; State s1(state); move(s1.a, s1.b, B); newStates.insert(s1); State s2(state); move(s2.a, s2.c, C); newStates.insert(s2); State s3(state); move(s3.b, s3.a, A); newStates.insert(s3); State s4(state); move(s4.b, s4.c, C); newStates.insert(s4); State s5(state); move(s5.c, s5.a, A); newStates.insert(s5); State s6(state); move(s6.c, s6.b, B); newStates.insert(s6); } Q.clear(); for(set<State>::iterator it = newStates.begin(); it != newStates.end(); ++it) { const State &state = *it; update_result(state, stage); if (used.find(state) == used.end()) { used.insert(state); Q.insert(state); } } } FOR(i,0,result.size()) printf("%d ", result[i]); printf("\n"); }
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 | #include <cstdio> #include <vector> #include <set> using namespace std; #define FOR(i,a,b) for(int (i)=(int)(a); (i)!=(int)(b); ++(i)) int A, B, C; vector<int> result; struct State { int a, b, c; bool operator<(const State &other) const { if (a != other.a) return a < other.a; if (b != other.b) return b < other.b; return c < other.c; } }; void update_result(const State &state, int stage) { if (result[state.a] == -1) result[state.a] = stage; if (result[state.b] == -1) result[state.b] = stage; if (result[state.c] == -1) result[state.c] = stage; } void move(int &a, int &b, int max_b) { if (a+b > max_b) { a -= max_b - b; b = max_b; } else { b += a; a = 0; } } set<State> Q, used; int main() { scanf("%d %d %d", &A, &B, &C); result.resize(C+1, -1); State init; scanf("%d %d %d", &init.a, &init.b, &init.c); update_result(init, 0); used.insert(init); Q.insert(init); int stage = 0; while (Q.size() > 0) { ++stage; set<State> newStates; for(set<State>::iterator it = Q.begin(); it != Q.end(); ++it) { const State &state = *it; State s1(state); move(s1.a, s1.b, B); newStates.insert(s1); State s2(state); move(s2.a, s2.c, C); newStates.insert(s2); State s3(state); move(s3.b, s3.a, A); newStates.insert(s3); State s4(state); move(s4.b, s4.c, C); newStates.insert(s4); State s5(state); move(s5.c, s5.a, A); newStates.insert(s5); State s6(state); move(s6.c, s6.b, B); newStates.insert(s6); } Q.clear(); for(set<State>::iterator it = newStates.begin(); it != newStates.end(); ++it) { const State &state = *it; update_result(state, stage); if (used.find(state) == used.end()) { used.insert(state); Q.insert(state); } } } FOR(i,0,result.size()) printf("%d ", result[i]); printf("\n"); } |