#include <iostream> #include <vector> #include <algorithm> #include <functional> #include <iterator> #include <bitset> #include <set> #include <utility> #include <tuple> #include <queue> using V = std::vector<int>; V addV(int bFrom, int bTo, const V& bottles,V v) { // std::cout<<"TYGRYS add"<< bFrom<<bTo<<std::endl; int x = std::min(v[bFrom], bottles[bTo]-v[bTo]); v[bFrom]-=x; v[bTo]+=x; return v; } std::vector<V> getAdj(const V& bottles,const V& v) { std::vector<V> vs; if(v[0] and bottles[1]>v[1]) vs.push_back(addV(0,1,bottles, v)); if(v[0] and bottles[2]>v[2]) vs.push_back(addV(0,2,bottles, v)); if(v[1] and bottles[2]>v[2]) vs.push_back(addV(1,2, bottles, v)); if(v[1] and bottles[0]>v[0]) vs.push_back(addV(1,0, bottles, v)); if(v[2] and bottles[1]>v[1]) vs.push_back(addV(2,1, bottles, v)); if(v[2] and bottles[0]>v[0]) vs.push_back(addV(2,0, bottles, v)); return vs; } void count(V bottles, V v, std::vector<int>& numberOfMoves, int n) { std::queue<std::pair<V,int>> q; std::set<V, std::less<>> discovered; discovered.insert(v); numberOfMoves[v[0]] = 0; numberOfMoves[v[1]] = 0; numberOfMoves[v[2]] = 0; q.push({v,1}); while (!q.empty()) { auto qq = q.front(); int move = qq.second; v =qq.first; q.pop(); const auto& adj = getAdj(bottles, v); for (V u: adj) { numberOfMoves[u[0]] = std::min(move, numberOfMoves[u[0]]); numberOfMoves[u[1]] = std::min(move, numberOfMoves[u[1]]); numberOfMoves[u[2]] = std::min(move, numberOfMoves[u[2]]); if (discovered.find(u) == discovered.end()) { discovered.insert(u); q.push({u, move+1}); } } } } int main() { std::ios_base::sync_with_stdio(false); V start(3,0), bottles(3,0); for(int i=0;i<3;++i) std::cin>>bottles[i]; for(int i=0;i<3;++i) std::cin>>start[i]; std::vector<int> numberOfMoves(bottles[2]+1, 1000000000); count(bottles, start, numberOfMoves,0); for(auto &x:numberOfMoves) if(x==1000000000) x=-1; for(const auto& num:numberOfMoves) std::cout<<num<<" "; 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 | #include <iostream> #include <vector> #include <algorithm> #include <functional> #include <iterator> #include <bitset> #include <set> #include <utility> #include <tuple> #include <queue> using V = std::vector<int>; V addV(int bFrom, int bTo, const V& bottles,V v) { // std::cout<<"TYGRYS add"<< bFrom<<bTo<<std::endl; int x = std::min(v[bFrom], bottles[bTo]-v[bTo]); v[bFrom]-=x; v[bTo]+=x; return v; } std::vector<V> getAdj(const V& bottles,const V& v) { std::vector<V> vs; if(v[0] and bottles[1]>v[1]) vs.push_back(addV(0,1,bottles, v)); if(v[0] and bottles[2]>v[2]) vs.push_back(addV(0,2,bottles, v)); if(v[1] and bottles[2]>v[2]) vs.push_back(addV(1,2, bottles, v)); if(v[1] and bottles[0]>v[0]) vs.push_back(addV(1,0, bottles, v)); if(v[2] and bottles[1]>v[1]) vs.push_back(addV(2,1, bottles, v)); if(v[2] and bottles[0]>v[0]) vs.push_back(addV(2,0, bottles, v)); return vs; } void count(V bottles, V v, std::vector<int>& numberOfMoves, int n) { std::queue<std::pair<V,int>> q; std::set<V, std::less<>> discovered; discovered.insert(v); numberOfMoves[v[0]] = 0; numberOfMoves[v[1]] = 0; numberOfMoves[v[2]] = 0; q.push({v,1}); while (!q.empty()) { auto qq = q.front(); int move = qq.second; v =qq.first; q.pop(); const auto& adj = getAdj(bottles, v); for (V u: adj) { numberOfMoves[u[0]] = std::min(move, numberOfMoves[u[0]]); numberOfMoves[u[1]] = std::min(move, numberOfMoves[u[1]]); numberOfMoves[u[2]] = std::min(move, numberOfMoves[u[2]]); if (discovered.find(u) == discovered.end()) { discovered.insert(u); q.push({u, move+1}); } } } } int main() { std::ios_base::sync_with_stdio(false); V start(3,0), bottles(3,0); for(int i=0;i<3;++i) std::cin>>bottles[i]; for(int i=0;i<3;++i) std::cin>>start[i]; std::vector<int> numberOfMoves(bottles[2]+1, 1000000000); count(bottles, start, numberOfMoves,0); for(auto &x:numberOfMoves) if(x==1000000000) x=-1; for(const auto& num:numberOfMoves) std::cout<<num<<" "; return 0; } |