#include<iostream> #include<string> #include<vector> #include<algorithm> #include<list> #include<set> using namespace std; class Bottle { public: int a, b, c; Bottle() { } Bottle(int a, int b, int c) { this->a = a; this->b = b; this->c = c; } }; class Step { public: int steps; Bottle bottle; Step(int step, Bottle bottle) { this->steps = step; this->bottle = bottle; } }; struct cmp { bool operator() (const Bottle& i, const Bottle& j) const { long long l = i.a * 1000000000000 + i.b * 1000000 + i.c; long long r = j.a * 1000000000000 + j.b * 1000000 + j.c; return l < r; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int A, B, C, a, b, c; cin >> A >> B >> C >> a >> b >> c; vector<long long> tab(C + 1, -1); Bottle bottle(a, b, c); set<Bottle, cmp> checked; checked.insert(bottle); Step step(0, bottle); list<Step> queue; queue.push_back(step); while (!queue.empty()) { Step step = queue.front(); queue.pop_front(); if (tab[step.bottle.a] == -1) { tab[step.bottle.a] = step.steps; } if (tab[step.bottle.b] == -1) { tab[step.bottle.b] = step.steps; } if (tab[step.bottle.c] == -1) { tab[step.bottle.c] = step.steps; } Bottle ab = Bottle(step.bottle.a - min(B - step.bottle.b, step.bottle.a), step.bottle.b + min(B - step.bottle.b, step.bottle.a), step.bottle.c); // a -> b if (checked.find(ab) == checked.end()) { checked.insert(ab); queue.push_back(Step(step.steps + 1, ab)); } Bottle ac = Bottle(step.bottle.a - min(C - step.bottle.c, step.bottle.a), step.bottle.b, step.bottle.c + min(C - step.bottle.c, step.bottle.a)); // a -> c if (checked.find(ac) == checked.end()) { checked.insert(ac); queue.push_back(Step(step.steps + 1, ac)); } Bottle bc = Bottle(step.bottle.a, step.bottle.b - min(C - step.bottle.c, step.bottle.b), step.bottle.c + min(C - step.bottle.c, step.bottle.b)); // b -> c if (checked.find(bc) == checked.end()) { checked.insert(bc); queue.push_back(Step(step.steps + 1, bc)); } Bottle ba = Bottle(step.bottle.a + min(A - step.bottle.a, step.bottle.b), step.bottle.b - min(A - step.bottle.a, step.bottle.b), step.bottle.c); // b -> a if (checked.find(ba) == checked.end()) { checked.insert(ba); queue.push_back(Step(step.steps + 1, ba)); } Bottle ca = Bottle(step.bottle.a + min(A - step.bottle.a, step.bottle.c), step.bottle.b, step.bottle.c - min(A - step.bottle.a, step.bottle.c)); // c -> a if (checked.find(ca) == checked.end()) { checked.insert(ca); queue.push_back(Step(step.steps + 1, ca)); } Bottle cb = Bottle(step.bottle.a, step.bottle.b + min(B - step.bottle.b, step.bottle.c), step.bottle.c - min(B - step.bottle.b, step.bottle.c)); // c -> b if (checked.find(cb) == checked.end()) { checked.insert(cb); queue.push_back(Step(step.steps + 1, cb)); } } for (int i = 0; i < tab.size(); i++) { cout << tab[i] << " "; } 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 103 104 105 106 107 108 109 | #include<iostream> #include<string> #include<vector> #include<algorithm> #include<list> #include<set> using namespace std; class Bottle { public: int a, b, c; Bottle() { } Bottle(int a, int b, int c) { this->a = a; this->b = b; this->c = c; } }; class Step { public: int steps; Bottle bottle; Step(int step, Bottle bottle) { this->steps = step; this->bottle = bottle; } }; struct cmp { bool operator() (const Bottle& i, const Bottle& j) const { long long l = i.a * 1000000000000 + i.b * 1000000 + i.c; long long r = j.a * 1000000000000 + j.b * 1000000 + j.c; return l < r; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int A, B, C, a, b, c; cin >> A >> B >> C >> a >> b >> c; vector<long long> tab(C + 1, -1); Bottle bottle(a, b, c); set<Bottle, cmp> checked; checked.insert(bottle); Step step(0, bottle); list<Step> queue; queue.push_back(step); while (!queue.empty()) { Step step = queue.front(); queue.pop_front(); if (tab[step.bottle.a] == -1) { tab[step.bottle.a] = step.steps; } if (tab[step.bottle.b] == -1) { tab[step.bottle.b] = step.steps; } if (tab[step.bottle.c] == -1) { tab[step.bottle.c] = step.steps; } Bottle ab = Bottle(step.bottle.a - min(B - step.bottle.b, step.bottle.a), step.bottle.b + min(B - step.bottle.b, step.bottle.a), step.bottle.c); // a -> b if (checked.find(ab) == checked.end()) { checked.insert(ab); queue.push_back(Step(step.steps + 1, ab)); } Bottle ac = Bottle(step.bottle.a - min(C - step.bottle.c, step.bottle.a), step.bottle.b, step.bottle.c + min(C - step.bottle.c, step.bottle.a)); // a -> c if (checked.find(ac) == checked.end()) { checked.insert(ac); queue.push_back(Step(step.steps + 1, ac)); } Bottle bc = Bottle(step.bottle.a, step.bottle.b - min(C - step.bottle.c, step.bottle.b), step.bottle.c + min(C - step.bottle.c, step.bottle.b)); // b -> c if (checked.find(bc) == checked.end()) { checked.insert(bc); queue.push_back(Step(step.steps + 1, bc)); } Bottle ba = Bottle(step.bottle.a + min(A - step.bottle.a, step.bottle.b), step.bottle.b - min(A - step.bottle.a, step.bottle.b), step.bottle.c); // b -> a if (checked.find(ba) == checked.end()) { checked.insert(ba); queue.push_back(Step(step.steps + 1, ba)); } Bottle ca = Bottle(step.bottle.a + min(A - step.bottle.a, step.bottle.c), step.bottle.b, step.bottle.c - min(A - step.bottle.a, step.bottle.c)); // c -> a if (checked.find(ca) == checked.end()) { checked.insert(ca); queue.push_back(Step(step.steps + 1, ca)); } Bottle cb = Bottle(step.bottle.a, step.bottle.b + min(B - step.bottle.b, step.bottle.c), step.bottle.c - min(B - step.bottle.b, step.bottle.c)); // c -> b if (checked.find(cb) == checked.end()) { checked.insert(cb); queue.push_back(Step(step.steps + 1, cb)); } } for (int i = 0; i < tab.size(); i++) { cout << tab[i] << " "; } return 0; } |