#include <iostream> #include <unordered_set> #include <vector> #include <deque> using namespace std; void pour(int A, int B, int a, int b, int &ao, int &bo) { (void)A; int x = min(a, B-b); ao = a - x; bo = b + x; } void push(deque<pair<pair<int,int>,int>> &Q, int a, int b, int d) { Q.push_back({{a, b}, d}); } int main() { int A, B, C; int a, b, c; cin >> A >> B >> C; cin >> a >> b >> c; int t = a+b+c; auto hasher = [](const pair<int,int> &p) { return p.first * 131072 + p.second; }; unordered_set<pair<int,int>,decltype(hasher)> V(0, hasher); deque<pair<pair<int,int>,int>> Q; Q.push_back({{a,b}, 0}); vector<int> S(C+1, -1); while (!Q.empty()) { auto s = Q.front(); Q.pop_front(); auto i = V.find(s.first); if (i != V.end()) continue; V.insert(s.first); a = s.first.first; b = s.first.second; c = t - a - b; if (S[a] == -1 || S[a] > s.second) S[a] = s.second; if (S[b] == -1 || S[b] > s.second) S[b] = s.second; if (S[c] == -1 || S[c] > s.second) S[c] = s.second; int na, nb, nc; pour(A, B, a, b, na, nb); push(Q, na, nb, s.second+1); pour(B, A, b, a, nb, na); push(Q, na, nb, s.second+1); pour(A, C, a, c, na, nc); push(Q, na, b, s.second+1); pour(C, A, c, a, nc, na); push(Q, na, b, s.second+1); pour(B, C, b, c, nb, nc); push(Q, a, nb, s.second+1); pour(C, B, c, b, nc, nb); push(Q, a, nb, s.second+1); } for (int i=0; i<C; i++) cout << S[i] << " "; cout << S[C] << "\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 | #include <iostream> #include <unordered_set> #include <vector> #include <deque> using namespace std; void pour(int A, int B, int a, int b, int &ao, int &bo) { (void)A; int x = min(a, B-b); ao = a - x; bo = b + x; } void push(deque<pair<pair<int,int>,int>> &Q, int a, int b, int d) { Q.push_back({{a, b}, d}); } int main() { int A, B, C; int a, b, c; cin >> A >> B >> C; cin >> a >> b >> c; int t = a+b+c; auto hasher = [](const pair<int,int> &p) { return p.first * 131072 + p.second; }; unordered_set<pair<int,int>,decltype(hasher)> V(0, hasher); deque<pair<pair<int,int>,int>> Q; Q.push_back({{a,b}, 0}); vector<int> S(C+1, -1); while (!Q.empty()) { auto s = Q.front(); Q.pop_front(); auto i = V.find(s.first); if (i != V.end()) continue; V.insert(s.first); a = s.first.first; b = s.first.second; c = t - a - b; if (S[a] == -1 || S[a] > s.second) S[a] = s.second; if (S[b] == -1 || S[b] > s.second) S[b] = s.second; if (S[c] == -1 || S[c] > s.second) S[c] = s.second; int na, nb, nc; pour(A, B, a, b, na, nb); push(Q, na, nb, s.second+1); pour(B, A, b, a, nb, na); push(Q, na, nb, s.second+1); pour(A, C, a, c, na, nc); push(Q, na, b, s.second+1); pour(C, A, c, a, nc, na); push(Q, na, b, s.second+1); pour(B, C, b, c, nb, nc); push(Q, a, nb, s.second+1); pour(C, B, c, b, nc, nb); push(Q, a, nb, s.second+1); } for (int i=0; i<C; i++) cout << S[i] << " "; cout << S[C] << "\n"; return 0; } |