#include <iostream> #include <vector> #include <queue> #include <set> #include <string> #include <sstream> #include <cstring> #include <cstdlib> #include <algorithm> #include <limits.h> using namespace std; struct buckets{ int a; int b; int c; }; struct state{ buckets b; int step; }; inline bool operator<(const buckets x, const buckets y){ return x.a < y.a || (x.a == y.a && x.b < y.b ) || (x.a == y.a && x.b == y.b && x.c < y.c); } int main(){ int a,b,c, max_a, max_b, max_c; cin >> max_a >> max_b >> max_c >> a >> b >> c; queue<state> states; set<buckets> generated; vector<int> min_steps(max_c + 1, -1); state s = {{a,b,c},0}; states.push(s); generated.insert({a,b,c}); while(!states.empty()) { // cout << s.step; s = states.front(); states.pop(); a = s.b.a; b = s.b.b; c = s.b.c; if(min_steps[a] == -1) min_steps[a] = s.step; if(min_steps[b] == -1) min_steps[b] = s.step; if(min_steps[c] == -1) min_steps[c] = s.step; buckets ab = {max(0, a - max_b + b), min(max_b, b + a), c }; buckets ac = {max(0, a - max_c + c), b, min(max_c, c + a) }; buckets ba = {min(max_a, a + b), max(0, b - max_a + a), c }; buckets bc = {a, max(0, b - max_c + c), min(max_c, c + b) }; buckets ca = {min(max_a, a + c), b, max(0, c - max_a + a) }; buckets cb = {a, min(max_b, b + c), max(0, c - max_b + b) }; if(generated.find(ab) == generated.end()) { generated.insert(ab); states.push({ab, s.step + 1});} if(generated.find(ac) == generated.end()) { generated.insert(ac); states.push({ac, s.step + 1});} if(generated.find(ba) == generated.end()) { generated.insert(ba); states.push({ba, s.step + 1});} if(generated.find(bc) == generated.end()) { generated.insert(bc); states.push({bc, s.step + 1});} if(generated.find(ca) == generated.end()) { generated.insert(ca); states.push({ca, s.step + 1});} if(generated.find(cb) == generated.end()) { generated.insert(cb); states.push({cb, s.step + 1});} } for(int i = 0; i <= max_c; i++) cout << min_steps[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 | #include <iostream> #include <vector> #include <queue> #include <set> #include <string> #include <sstream> #include <cstring> #include <cstdlib> #include <algorithm> #include <limits.h> using namespace std; struct buckets{ int a; int b; int c; }; struct state{ buckets b; int step; }; inline bool operator<(const buckets x, const buckets y){ return x.a < y.a || (x.a == y.a && x.b < y.b ) || (x.a == y.a && x.b == y.b && x.c < y.c); } int main(){ int a,b,c, max_a, max_b, max_c; cin >> max_a >> max_b >> max_c >> a >> b >> c; queue<state> states; set<buckets> generated; vector<int> min_steps(max_c + 1, -1); state s = {{a,b,c},0}; states.push(s); generated.insert({a,b,c}); while(!states.empty()) { // cout << s.step; s = states.front(); states.pop(); a = s.b.a; b = s.b.b; c = s.b.c; if(min_steps[a] == -1) min_steps[a] = s.step; if(min_steps[b] == -1) min_steps[b] = s.step; if(min_steps[c] == -1) min_steps[c] = s.step; buckets ab = {max(0, a - max_b + b), min(max_b, b + a), c }; buckets ac = {max(0, a - max_c + c), b, min(max_c, c + a) }; buckets ba = {min(max_a, a + b), max(0, b - max_a + a), c }; buckets bc = {a, max(0, b - max_c + c), min(max_c, c + b) }; buckets ca = {min(max_a, a + c), b, max(0, c - max_a + a) }; buckets cb = {a, min(max_b, b + c), max(0, c - max_b + b) }; if(generated.find(ab) == generated.end()) { generated.insert(ab); states.push({ab, s.step + 1});} if(generated.find(ac) == generated.end()) { generated.insert(ac); states.push({ac, s.step + 1});} if(generated.find(ba) == generated.end()) { generated.insert(ba); states.push({ba, s.step + 1});} if(generated.find(bc) == generated.end()) { generated.insert(bc); states.push({bc, s.step + 1});} if(generated.find(ca) == generated.end()) { generated.insert(ca); states.push({ca, s.step + 1});} if(generated.find(cb) == generated.end()) { generated.insert(cb); states.push({cb, s.step + 1});} } for(int i = 0; i <= max_c; i++) cout << min_steps[i] << " "; } |