#include <bits/stdc++.h> using namespace std; using LL = long long; #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) #define ssize(x) int(x.size()) template<class A, class B> auto& operator<<(ostream &o, pair<A, B> p) { return o << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) { o << '{'; int i = 0; for(auto e : x) o << (", ")+2*!i++ << e; return o << '}'; } #ifdef DEBUG #define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n' #else #define debug(...) {} #endif int main() { cin.tie(0)->sync_with_stdio(0); array<int, 3> volume; array<int, 3> cur; REP(i, 3) cin >> volume[i]; REP(i, 3) cin >> cur[i]; int sum = cur[0] + cur[1] + cur[2]; int inf = 1e9; vector dst(3, vector(2, vector(volume[2] + 1, inf))); auto encode = [&](array<int, 3> state) { REP(i, 3) { int j = (i + 1) % 3; if(state[i] == 0) return tuple {i, 0, state[j]}; if(state[i] == volume[i]) return tuple {i, 1, state[j]}; } assert(false); }; auto decode = [&](int i, int is_full, int s_nxt) { array<int, 3> state = {0, 0, 0}; int j = (i + 1) % 3; state[i] = (is_full ? volume[i] : 0); state[j] = s_nxt; state[3 ^ i ^ j] = sum - state[i] - state[j]; return state; }; vector<int> ans(volume[2] + 1, inf); deque<tuple<int, int, int>> Q; auto trans = [&](array<int, 3> state, int d) { REP(i, 3) ans[state[i]] = min(ans[state[i]], d); REP(i, 3) REP(j, 3) if(i != j) { auto s = state; int delta = min(s[i], volume[j] - s[j]); s[i] -= delta; s[j] += delta; auto [x, y, z] = encode(s); if(dst[x][y][z] > d + 1) { dst[x][y][z] = d + 1; Q.emplace_back(x, y, z); } } }; trans(cur, 0); while(!Q.empty()) { auto [x, y, z] = Q.front(); Q.pop_front(); auto state = decode(x, y, z); trans(state, dst[x][y][z]); } for(int x : ans) cout << (x == inf ? -1 : x) << " "; cout << "\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 77 78 79 80 81 | #include <bits/stdc++.h> using namespace std; using LL = long long; #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) #define ssize(x) int(x.size()) template<class A, class B> auto& operator<<(ostream &o, pair<A, B> p) { return o << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) { o << '{'; int i = 0; for(auto e : x) o << (", ")+2*!i++ << e; return o << '}'; } #ifdef DEBUG #define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n' #else #define debug(...) {} #endif int main() { cin.tie(0)->sync_with_stdio(0); array<int, 3> volume; array<int, 3> cur; REP(i, 3) cin >> volume[i]; REP(i, 3) cin >> cur[i]; int sum = cur[0] + cur[1] + cur[2]; int inf = 1e9; vector dst(3, vector(2, vector(volume[2] + 1, inf))); auto encode = [&](array<int, 3> state) { REP(i, 3) { int j = (i + 1) % 3; if(state[i] == 0) return tuple {i, 0, state[j]}; if(state[i] == volume[i]) return tuple {i, 1, state[j]}; } assert(false); }; auto decode = [&](int i, int is_full, int s_nxt) { array<int, 3> state = {0, 0, 0}; int j = (i + 1) % 3; state[i] = (is_full ? volume[i] : 0); state[j] = s_nxt; state[3 ^ i ^ j] = sum - state[i] - state[j]; return state; }; vector<int> ans(volume[2] + 1, inf); deque<tuple<int, int, int>> Q; auto trans = [&](array<int, 3> state, int d) { REP(i, 3) ans[state[i]] = min(ans[state[i]], d); REP(i, 3) REP(j, 3) if(i != j) { auto s = state; int delta = min(s[i], volume[j] - s[j]); s[i] -= delta; s[j] += delta; auto [x, y, z] = encode(s); if(dst[x][y][z] > d + 1) { dst[x][y][z] = d + 1; Q.emplace_back(x, y, z); } } }; trans(cur, 0); while(!Q.empty()) { auto [x, y, z] = Q.front(); Q.pop_front(); auto state = decode(x, y, z); trans(state, dst[x][y][z]); } for(int x : ans) cout << (x == inf ? -1 : x) << " "; cout << "\n"; } |