#include <iostream> #include <vector> #include <queue> #include <set> using namespace std; struct triple{ vector<int> MAX; vector<int> vals; triple(int maxA, int maxB, int maxC, int a, int b, int c) { MAX.push_back(maxA), MAX.push_back(maxB), MAX.push_back(maxC); vals.push_back(a), vals.push_back(b), vals.push_back(c); } int& operator[](int i) { return vals[i]; } triple getNext(int i, int j) { triple res = *this; int a = vals[i], b = vals[j], c = a + b; if(c > MAX[j]) { a += c - MAX[j]; c = MAX[j]; } a -= vals[i]; res[i] = a, res[j] = c; return res; } friend bool operator<(const triple& A, const triple& B) { return A.vals < B.vals; } friend bool operator>(const triple& A, const triple& B) { return A.vals > B.vals; } friend bool operator==(const triple& A, const triple& B) { return A.vals == B.vals; } }; void print(triple a) { cout << a[0] << " " << a[1] << " " << a[2] << "\n"; } int main() { ios_base::sync_with_stdio(0); int maxA, maxB, maxC, a, b, c; cin >> maxA >> maxB >> maxC >> a >> b >> c; triple T(maxA, maxB, maxC, a, b, c); queue<pair<triple, int> > Q; Q.push({T, 0}); set<triple> S; S.insert(T); vector<int> res(maxC + 1, -1); while(!Q.empty()) { pair<triple, int> P = Q.front(); Q.pop(); if(P.second > maxC) { break; } for(int i = 0; i < 3; ++i){ if(res[P.first[i]] == -1) { res[P.first[i]] = P.second; } } //print(P.first); for(int i = 0; i < 3; ++i) { for(int j = 0; j < 3; ++j) { if(i != j) { triple next = P.first.getNext(i, j); if(S.find(next) == S.end()) { Q.push({next, P.second + 1}); S.insert(next); } } } } } for(int i = 0; i <= maxC; ++i) { cout << res[i] << " "; } cout << "\n"; }
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 | #include <iostream> #include <vector> #include <queue> #include <set> using namespace std; struct triple{ vector<int> MAX; vector<int> vals; triple(int maxA, int maxB, int maxC, int a, int b, int c) { MAX.push_back(maxA), MAX.push_back(maxB), MAX.push_back(maxC); vals.push_back(a), vals.push_back(b), vals.push_back(c); } int& operator[](int i) { return vals[i]; } triple getNext(int i, int j) { triple res = *this; int a = vals[i], b = vals[j], c = a + b; if(c > MAX[j]) { a += c - MAX[j]; c = MAX[j]; } a -= vals[i]; res[i] = a, res[j] = c; return res; } friend bool operator<(const triple& A, const triple& B) { return A.vals < B.vals; } friend bool operator>(const triple& A, const triple& B) { return A.vals > B.vals; } friend bool operator==(const triple& A, const triple& B) { return A.vals == B.vals; } }; void print(triple a) { cout << a[0] << " " << a[1] << " " << a[2] << "\n"; } int main() { ios_base::sync_with_stdio(0); int maxA, maxB, maxC, a, b, c; cin >> maxA >> maxB >> maxC >> a >> b >> c; triple T(maxA, maxB, maxC, a, b, c); queue<pair<triple, int> > Q; Q.push({T, 0}); set<triple> S; S.insert(T); vector<int> res(maxC + 1, -1); while(!Q.empty()) { pair<triple, int> P = Q.front(); Q.pop(); if(P.second > maxC) { break; } for(int i = 0; i < 3; ++i){ if(res[P.first[i]] == -1) { res[P.first[i]] = P.second; } } //print(P.first); for(int i = 0; i < 3; ++i) { for(int j = 0; j < 3; ++j) { if(i != j) { triple next = P.first.getNext(i, j); if(S.find(next) == S.end()) { Q.push({next, P.second + 1}); S.insert(next); } } } } } for(int i = 0; i <= maxC; ++i) { cout << res[i] << " "; } cout << "\n"; } |