#include <iostream> #include <queue> #include <map> #include <vector> using namespace std; int S[3]; struct TriBottle { int b[3]; TriBottle move_water(int from, int to) { int delta = min(b[from], S[to]-b[to]); TriBottle res(*this); res.b[from] -= delta; res.b[to] += delta; return res; } bool operator<(const TriBottle &tb) const{ if (b[0]!=tb.b[0]) return b[0] < tb.b[0]; if (b[1]!=tb.b[1]) return b[1] < tb.b[1]; return b[2] < tb.b[2]; } }; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int a,b,c; cin >> S[0] >> S[1] >> S[2] >> a >> b >> c; map<TriBottle, int>Mp; queue<TriBottle>Q; Mp[TriBottle{.b={a, b, c}}] = 1; Q.push(TriBottle{.b={a, b, c}}); vector<int>Results(S[2]+1, 0); while(!Q.empty()) { TriBottle tb = Q.front(); Q.pop(); for(int b=0;b<3;b++) if (Results[tb.b[b]] == 0) Results[tb.b[b]] = Mp[tb]; for(int from=0;from<3;from++) for(int to=0;to<3;to++) if(from!=to){ TriBottle after = tb.move_water(from, to); if (Mp[after]==0) { Mp[after] = Mp[tb]+1; Q.push(after); } } } for(int x : Results) cout << x-1 << " "; cout << "\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 60 61 62 63 64 65 66 67 | #include <iostream> #include <queue> #include <map> #include <vector> using namespace std; int S[3]; struct TriBottle { int b[3]; TriBottle move_water(int from, int to) { int delta = min(b[from], S[to]-b[to]); TriBottle res(*this); res.b[from] -= delta; res.b[to] += delta; return res; } bool operator<(const TriBottle &tb) const{ if (b[0]!=tb.b[0]) return b[0] < tb.b[0]; if (b[1]!=tb.b[1]) return b[1] < tb.b[1]; return b[2] < tb.b[2]; } }; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int a,b,c; cin >> S[0] >> S[1] >> S[2] >> a >> b >> c; map<TriBottle, int>Mp; queue<TriBottle>Q; Mp[TriBottle{.b={a, b, c}}] = 1; Q.push(TriBottle{.b={a, b, c}}); vector<int>Results(S[2]+1, 0); while(!Q.empty()) { TriBottle tb = Q.front(); Q.pop(); for(int b=0;b<3;b++) if (Results[tb.b[b]] == 0) Results[tb.b[b]] = Mp[tb]; for(int from=0;from<3;from++) for(int to=0;to<3;to++) if(from!=to){ TriBottle after = tb.move_water(from, to); if (Mp[after]==0) { Mp[after] = Mp[tb]+1; Q.push(after); } } } for(int x : Results) cout << x-1 << " "; cout << "\n"; return 0; } |