#include <iostream> #include <vector> #include <set> #include <queue> #include <array> using namespace std; int A, B, C, a, b, c; struct State { int a, b, c, dist; State transfer(int from, int to) { State ret = *this; array<int, 3> tmp{a, b, c}, TMP{A, B, C}; if (tmp[from] < TMP[to] - tmp[to]) { tmp[to] += tmp[from]; tmp[from] = 0; } else { tmp[from] -= TMP[to] - tmp[to]; tmp[to] = TMP[to]; } ret.a = tmp[0]; ret.b = tmp[1]; ret.c = tmp[2]; ret.dist = dist + 1; return ret; } }; bool operator<(const State &s1, const State &s2) { if (s1.a < s2.a) return true; if (s1.a > s2.a) return false; if (s1.b < s2.b) return true; if (s1.b > s2.b) return false; if (s1.c < s2.c) return true; if (s1.c > s2.c) return false; return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> A >> B >> C >> a >> b >> c; vector<int> res; res.resize(C + 1, -1); queue<State> queue; set<State> visited; queue.push({a, b, c, 0}); visited.insert({a, b, c, 0}); while (!queue.empty()) { auto e = queue.front(); queue.pop(); if (res[e.a] == -1) res[e.a] = e.dist; if (res[e.b] == -1) res[e.b] = e.dist; if (res[e.c] == -1) res[e.c] = e.dist; for (auto [from, to] : {pair<int, int>{0, 1}, {0, 2}, {1, 0}, {1, 2}, {2, 0}, {2, 1}}) { auto s = e.transfer(from, to); if (visited.find(s) == visited.end()) { queue.push(s); visited.insert(s); } } } for (auto e : res) { cout << e << ' '; } }
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 | #include <iostream> #include <vector> #include <set> #include <queue> #include <array> using namespace std; int A, B, C, a, b, c; struct State { int a, b, c, dist; State transfer(int from, int to) { State ret = *this; array<int, 3> tmp{a, b, c}, TMP{A, B, C}; if (tmp[from] < TMP[to] - tmp[to]) { tmp[to] += tmp[from]; tmp[from] = 0; } else { tmp[from] -= TMP[to] - tmp[to]; tmp[to] = TMP[to]; } ret.a = tmp[0]; ret.b = tmp[1]; ret.c = tmp[2]; ret.dist = dist + 1; return ret; } }; bool operator<(const State &s1, const State &s2) { if (s1.a < s2.a) return true; if (s1.a > s2.a) return false; if (s1.b < s2.b) return true; if (s1.b > s2.b) return false; if (s1.c < s2.c) return true; if (s1.c > s2.c) return false; return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> A >> B >> C >> a >> b >> c; vector<int> res; res.resize(C + 1, -1); queue<State> queue; set<State> visited; queue.push({a, b, c, 0}); visited.insert({a, b, c, 0}); while (!queue.empty()) { auto e = queue.front(); queue.pop(); if (res[e.a] == -1) res[e.a] = e.dist; if (res[e.b] == -1) res[e.b] = e.dist; if (res[e.c] == -1) res[e.c] = e.dist; for (auto [from, to] : {pair<int, int>{0, 1}, {0, 2}, {1, 0}, {1, 2}, {2, 0}, {2, 1}}) { auto s = e.transfer(from, to); if (visited.find(s) == visited.end()) { queue.push(s); visited.insert(s); } } } for (auto e : res) { cout << e << ' '; } } |