#include<bits/stdc++.h> #define VAR(i,n) __typeof(n) i = (n) #define loop(i,j,s) for(int i=j;i<s;i++) #define loopback(i,j,s) for(int i=j;i>=s;i--) #define foreach(i,c) for(VAR(i,(c).begin());i!=(c).end();i++) #define pln( x ) cout << x << "\n" #define ps( x ) cout << x << " " #define entr cout << "\n" #define pcnt(i) __builtin_popcount(i) #define ll long long #define pb push_back #define mp make_pair #define ff first #define ss second #define SIZE(c) (c).size() #define ALL(c) (c).begin(), (c).end() using namespace std; typedef vector<int> vi; typedef pair<int, int> PII; typedef vector<vector<int> > vvi; const int INFTY=20000000; const int MAX=500100; const int MOD=10000000; void coutTab(int* tab,int n){ loop(i,0,n){ cout<<tab[i]<<" "; } cout<<"\n"; } template<class T> void coutVec(vector<T> tab){ for(T t : tab){ cout<<t<<" "; } cout<<"\n"; } //------------------------------------------ struct State { int a,b,c; State(const State & s) : a(s.a), b(s.b), c(s.c) {}; State(int _a, int _b, int _c) : a(_a), b(_b), c(_c) {}; bool operator<(const State & s) const { if(a==s.a && b == s.b) return c < s.c; if(a==s.a) return b < s.b; return a < s.a; } bool operator==(const State & s) const { return a == s.a && b == s.b && c == s.c; } }; vi d; int A,B,C; bool not_in(const set<State> & A, State s) { return A.find(s) == A.end(); } void bfs(State sb) { queue<pair<State, int>> Q; Q.push({State(sb), 0}); set<State> vis; vis.insert(sb); while(!Q.empty()) { pair<State, int> q = Q.front(); Q.pop(); State s = q.ff; int cd = q.ss; //cout<<"D:"<<cd<<endl; if(cd==400005) continue; d[s.a] = d[s.a] == -1 ? cd : min(d[s.a], cd); d[s.b] = d[s.b] == -1 ? cd : min(d[s.b], cd); d[s.c] = d[s.c] == -1 ? cd : min(d[s.c], cd); int d12 = min(s.a, B-s.b); State s12 = State(s.a - d12, s.b + d12, s.c); int d13 = min(s.a, C-s.c); State s13 = State(s.a - d13, s.b, s.c + d13); int d21 = min(s.b, A-s.a); State s21 = State(s.a + d21, s.b - d21, s.c); int d23 = min(s.b, C-s.c); State s23 = State(s.a, s.b - d23, s.c + d23); int d31 = min(s.c, A-s.a); State s31 = State(s.a + d31, s.b, s.c - d31); int d32 = min(s.c, B-s.b); State s32 = State(s.a, s.b + d32, s.c - d32); State ss[] = { s12, s13, s21, s23, s31, s32 }; for(State ns : ss) { if(not_in(vis, ns)) { vis.insert(ns); Q.push({State(ns), cd + 1}); } } // if(not_in(vis, hsh(s12))) { // vis.insert(hsh(s12)); // Q.push({s12, cd + 1}); // } // if(not_in(vis, hsh(s13))) { // vis.insert(hsh(s13)); // Q.push({s13, cd + 1}); // } // if(not_in(vis, hsh(s21))) { // vis.insert(hsh(s21)); // Q.push({s21, cd + 1}); // } // if(not_in(vis, hsh(s23))) { // vis.insert(hsh(s23)); // Q.push({s23, cd + 1}); // } // if(not_in(vis, hsh(s31))) { // vis.insert(hsh(s31)); // Q.push({s31, cd + 1}); // } // if(not_in(vis, hsh(s32))) { // vis.insert(hsh(s32)); // Q.push({s32, cd + 1}); // } } } int main(){ ios_base::sync_with_stdio(0); int a,b,c; cin>>A>>B>>C; cin>>a>>b>>c; d = vi(C+1, -1); bfs(State(a,b,c)); loop(i,0,C+1) { ps(d[i]); } entr; }
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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | #include<bits/stdc++.h> #define VAR(i,n) __typeof(n) i = (n) #define loop(i,j,s) for(int i=j;i<s;i++) #define loopback(i,j,s) for(int i=j;i>=s;i--) #define foreach(i,c) for(VAR(i,(c).begin());i!=(c).end();i++) #define pln( x ) cout << x << "\n" #define ps( x ) cout << x << " " #define entr cout << "\n" #define pcnt(i) __builtin_popcount(i) #define ll long long #define pb push_back #define mp make_pair #define ff first #define ss second #define SIZE(c) (c).size() #define ALL(c) (c).begin(), (c).end() using namespace std; typedef vector<int> vi; typedef pair<int, int> PII; typedef vector<vector<int> > vvi; const int INFTY=20000000; const int MAX=500100; const int MOD=10000000; void coutTab(int* tab,int n){ loop(i,0,n){ cout<<tab[i]<<" "; } cout<<"\n"; } template<class T> void coutVec(vector<T> tab){ for(T t : tab){ cout<<t<<" "; } cout<<"\n"; } //------------------------------------------ struct State { int a,b,c; State(const State & s) : a(s.a), b(s.b), c(s.c) {}; State(int _a, int _b, int _c) : a(_a), b(_b), c(_c) {}; bool operator<(const State & s) const { if(a==s.a && b == s.b) return c < s.c; if(a==s.a) return b < s.b; return a < s.a; } bool operator==(const State & s) const { return a == s.a && b == s.b && c == s.c; } }; vi d; int A,B,C; bool not_in(const set<State> & A, State s) { return A.find(s) == A.end(); } void bfs(State sb) { queue<pair<State, int>> Q; Q.push({State(sb), 0}); set<State> vis; vis.insert(sb); while(!Q.empty()) { pair<State, int> q = Q.front(); Q.pop(); State s = q.ff; int cd = q.ss; //cout<<"D:"<<cd<<endl; if(cd==400005) continue; d[s.a] = d[s.a] == -1 ? cd : min(d[s.a], cd); d[s.b] = d[s.b] == -1 ? cd : min(d[s.b], cd); d[s.c] = d[s.c] == -1 ? cd : min(d[s.c], cd); int d12 = min(s.a, B-s.b); State s12 = State(s.a - d12, s.b + d12, s.c); int d13 = min(s.a, C-s.c); State s13 = State(s.a - d13, s.b, s.c + d13); int d21 = min(s.b, A-s.a); State s21 = State(s.a + d21, s.b - d21, s.c); int d23 = min(s.b, C-s.c); State s23 = State(s.a, s.b - d23, s.c + d23); int d31 = min(s.c, A-s.a); State s31 = State(s.a + d31, s.b, s.c - d31); int d32 = min(s.c, B-s.b); State s32 = State(s.a, s.b + d32, s.c - d32); State ss[] = { s12, s13, s21, s23, s31, s32 }; for(State ns : ss) { if(not_in(vis, ns)) { vis.insert(ns); Q.push({State(ns), cd + 1}); } } // if(not_in(vis, hsh(s12))) { // vis.insert(hsh(s12)); // Q.push({s12, cd + 1}); // } // if(not_in(vis, hsh(s13))) { // vis.insert(hsh(s13)); // Q.push({s13, cd + 1}); // } // if(not_in(vis, hsh(s21))) { // vis.insert(hsh(s21)); // Q.push({s21, cd + 1}); // } // if(not_in(vis, hsh(s23))) { // vis.insert(hsh(s23)); // Q.push({s23, cd + 1}); // } // if(not_in(vis, hsh(s31))) { // vis.insert(hsh(s31)); // Q.push({s31, cd + 1}); // } // if(not_in(vis, hsh(s32))) { // vis.insert(hsh(s32)); // Q.push({s32, cd + 1}); // } } } int main(){ ios_base::sync_with_stdio(0); int a,b,c; cin>>A>>B>>C; cin>>a>>b>>c; d = vi(C+1, -1); bfs(State(a,b,c)); loop(i,0,C+1) { ps(d[i]); } entr; } |