#include <iostream> #include <algorithm> #include <vector> #include <queue> #include <unordered_map> using namespace std; struct state_t { int v[3]; bool operator==(const state_t& other) const { return v[0]==other.v[0]&&v[1]==other.v[1]&&v[2]==other.v[2]; } }; namespace std { template <> struct hash<state_t> { size_t operator()(const state_t& k) const { return (size_t)k.v[0]+(size_t)k.v[1]+(size_t)k.v[2]; } }; } int V[3]; unordered_map<state_t,int> m; /* 2 7 9 1 3 6 3 3 3 2 2 3 */ struct entry_t { state_t state; int count; }; deque<entry_t> q; void visit(const state_t& s,const int count) { auto it=m.find(s); if(it!=m.end()) { if(it->second<=count) return; it->second=count; } else m.insert({s,count}); state_t copy; for(int i=0;i<3;++i) for(int j=0;j<3;++j) if(i!=j&&s.v[i]>0&&s.v[j]<V[j]) { copy = s; { const int moved=std::min(V[j]-s.v[j],s.v[i]); copy.v[i]-=moved; copy.v[j]+=moved; } q.push_back({copy,count+1}); } } int main() { cin>>V[0]>>V[1]>>V[2]; int a, b, c; cin>>a>>b>>c; q.push_back({{a,b,c},0}); while (!q.empty()) { const entry_t entry=q.front(); q.pop_front(); visit(entry.state,entry.count); } vector<int> result(V[2]+1,std::numeric_limits<int>::max()); for(auto it=m.begin();it!=m.end();++it) { result[it->first.v[0]]=std::min(result[it->first.v[0]],it->second); result[it->first.v[1]]=std::min(result[it->first.v[1]],it->second); result[it->first.v[2]]=std::min(result[it->first.v[2]],it->second); } for(int r:result) { const int outr=(r!=std::numeric_limits<int>::max())?r:-1; cout<<outr<<" "; } 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <unordered_map> using namespace std; struct state_t { int v[3]; bool operator==(const state_t& other) const { return v[0]==other.v[0]&&v[1]==other.v[1]&&v[2]==other.v[2]; } }; namespace std { template <> struct hash<state_t> { size_t operator()(const state_t& k) const { return (size_t)k.v[0]+(size_t)k.v[1]+(size_t)k.v[2]; } }; } int V[3]; unordered_map<state_t,int> m; /* 2 7 9 1 3 6 3 3 3 2 2 3 */ struct entry_t { state_t state; int count; }; deque<entry_t> q; void visit(const state_t& s,const int count) { auto it=m.find(s); if(it!=m.end()) { if(it->second<=count) return; it->second=count; } else m.insert({s,count}); state_t copy; for(int i=0;i<3;++i) for(int j=0;j<3;++j) if(i!=j&&s.v[i]>0&&s.v[j]<V[j]) { copy = s; { const int moved=std::min(V[j]-s.v[j],s.v[i]); copy.v[i]-=moved; copy.v[j]+=moved; } q.push_back({copy,count+1}); } } int main() { cin>>V[0]>>V[1]>>V[2]; int a, b, c; cin>>a>>b>>c; q.push_back({{a,b,c},0}); while (!q.empty()) { const entry_t entry=q.front(); q.pop_front(); visit(entry.state,entry.count); } vector<int> result(V[2]+1,std::numeric_limits<int>::max()); for(auto it=m.begin();it!=m.end();++it) { result[it->first.v[0]]=std::min(result[it->first.v[0]],it->second); result[it->first.v[1]]=std::min(result[it->first.v[1]],it->second); result[it->first.v[2]]=std::min(result[it->first.v[2]],it->second); } for(int r:result) { const int outr=(r!=std::numeric_limits<int>::max())?r:-1; cout<<outr<<" "; } return 0; } |