#include <iostream> #include <list> #include <vector> #include <map> using namespace std; #define y first #define x second typedef long long ll; typedef struct state{ int s[3]; int move=0; bool operator==(const state &o) const { return s[0] == o.s[0] && s[1] == o.s[1] && s[2] == o.s[2]; } bool operator<(const state &o) const { return s[0] < o.s[0] || (s[0] == o.s[0] && s[1] < o.s[1]) || (s[0] == o.s[0] && s[1] == o.s[1] && s[2] < o.s[2]); } }state; int main(){ int S[3]; int a, b, c; cin >> S[0] >> S[1] >> S[2]; cin >> a >> b >> c; vector<int> ans(S[2]+1, -1); int low = max(0, a+b+c-S[1]-S[2]); int hight = a+b+c; int cnt = hight-low+1; list<state> queue; state start; start.s[0] = a; start.s[1] = b; start.s[2] = c; start.move = 0; for(int i=0; i<3; i++){ if(ans[start.s[i]]==-1){ ans[start.s[i]] = start.move; cnt--; } } map<state, bool> visited; queue.push_back(start); while(cnt>0 && !queue.empty()){ state s = queue.front(); queue.pop_front(); for(int i=0; i<=2; i++){ for(int j=1; j<=2; j++){ int f = i; int t = (i+j)%3; //cout << s.s[0] << " " << s.s[1] << " " << s.s[2] <<"\n"; //cout << f << " " << t << "\n"; if(s.s[f]>0 && s.s[t]<S[t]){ int space = S[t] - s.s[t]; //cout << S[t] << " " << s.s[t] << " " << space << "\n"; state ns = s; ns.move = s.move +1; if(space>=s.s[f]){ ns.s[f]=0; ns.s[t]+=s.s[f]; }else{ ns.s[t] = S[t]; ns.s[f] -= space; } if(ans[ns.s[f]]==-1){ ans[ns.s[f]] = ns.move; cnt--; } if(ans[ns.s[t]]==-1){ ans[ns.s[t]] = ns.move; cnt--; } //cout << "NEW: " << ns.s[0] << " " << ns.s[1] << " " << ns.s[2] <<"\n"; if(!visited[ns]){ visited[ns] = true; queue.push_back(ns); } } } } } for(int i=0; i<=S[2]; i++){ cout << ans[i] << " "; } }
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 | #include <iostream> #include <list> #include <vector> #include <map> using namespace std; #define y first #define x second typedef long long ll; typedef struct state{ int s[3]; int move=0; bool operator==(const state &o) const { return s[0] == o.s[0] && s[1] == o.s[1] && s[2] == o.s[2]; } bool operator<(const state &o) const { return s[0] < o.s[0] || (s[0] == o.s[0] && s[1] < o.s[1]) || (s[0] == o.s[0] && s[1] == o.s[1] && s[2] < o.s[2]); } }state; int main(){ int S[3]; int a, b, c; cin >> S[0] >> S[1] >> S[2]; cin >> a >> b >> c; vector<int> ans(S[2]+1, -1); int low = max(0, a+b+c-S[1]-S[2]); int hight = a+b+c; int cnt = hight-low+1; list<state> queue; state start; start.s[0] = a; start.s[1] = b; start.s[2] = c; start.move = 0; for(int i=0; i<3; i++){ if(ans[start.s[i]]==-1){ ans[start.s[i]] = start.move; cnt--; } } map<state, bool> visited; queue.push_back(start); while(cnt>0 && !queue.empty()){ state s = queue.front(); queue.pop_front(); for(int i=0; i<=2; i++){ for(int j=1; j<=2; j++){ int f = i; int t = (i+j)%3; //cout << s.s[0] << " " << s.s[1] << " " << s.s[2] <<"\n"; //cout << f << " " << t << "\n"; if(s.s[f]>0 && s.s[t]<S[t]){ int space = S[t] - s.s[t]; //cout << S[t] << " " << s.s[t] << " " << space << "\n"; state ns = s; ns.move = s.move +1; if(space>=s.s[f]){ ns.s[f]=0; ns.s[t]+=s.s[f]; }else{ ns.s[t] = S[t]; ns.s[f] -= space; } if(ans[ns.s[f]]==-1){ ans[ns.s[f]] = ns.move; cnt--; } if(ans[ns.s[t]]==-1){ ans[ns.s[t]] = ns.move; cnt--; } //cout << "NEW: " << ns.s[0] << " " << ns.s[1] << " " << ns.s[2] <<"\n"; if(!visited[ns]){ visited[ns] = true; queue.push_back(ns); } } } } } for(int i=0; i<=S[2]; i++){ cout << ans[i] << " "; } } |