#include<iostream> #include<unordered_set> #include<queue> #include<utility> using namespace std; int A, B, C, a, b, c; #define ull unsigned long long struct phash { inline size_t operator()(const pair<int, int> &v) const { return v.first * 100003 + v.second; } }; unordered_set< pair<int, int>, phash > vis; queue< pair<pair<int, int>, int> > q; vector<int> reses; void generate_moves(int a, int b, int c, int res) { if(reses[a] == -1) { reses[a] = res; } if(reses[b] == -1) { reses[b] = res; } if(reses[c] == -1) { reses[c] = res; } if(a != 0) { if(b != B) { if((b + a) <= B) { pair<int, int> n = make_pair(0, a + b); if(!vis.count(n)) { vis.insert(n); pair<pair<int, int>, int> nn = make_pair(n, res + 1); q.push(nn); } } else { pair<int, int> n = make_pair(a + b - B, B); if(!vis.count(n)) { vis.insert(n); pair<pair<int, int>, int> nn = make_pair(n, res + 1); q.push(nn); } } } if(c != C) { if((c + a) <= C) { pair<int, int> n = make_pair(0, b); if(!vis.count(n)) { vis.insert(n); pair<pair<int, int>, int> nn = make_pair(n, res + 1); q.push(nn); } } else { pair<int, int> n = make_pair(a + c - C, b); if(!vis.count(n)) { vis.insert(n); pair<pair<int, int>, int> nn = make_pair(n, res + 1); q.push(nn); } } } } if(b != 0) { if(a != A) { if((b + a) <= A) { pair<int, int> n = make_pair(a + b, 0); if(!vis.count(n)) { vis.insert(n); pair<pair<int, int>, int> nn = make_pair(n, res + 1); q.push(nn); } } else { pair<int, int> n = make_pair(A, b + a - A); if(!vis.count(n)) { vis.insert(n); pair<pair<int, int>, int> nn = make_pair(n, res + 1); q.push(nn); } } } if(c != C) { if((c + b) <= C) { pair<int, int> n = make_pair(a, 0); if(!vis.count(n)) { vis.insert(n); pair<pair<int, int>, int> nn = make_pair(n, res + 1); q.push(nn); } } else { pair<int, int> n = make_pair(a, b + c - C); if(!vis.count(n)) { vis.insert(n); pair<pair<int, int>, int> nn = make_pair(n, res + 1); q.push(nn); } } } } if(c != 0) { if(a != A) { if((c + a) <= A) { pair<int, int> n = make_pair(c + a, b); if(!vis.count(n)) { vis.insert(n); pair<pair<int, int>, int> nn = make_pair(n, res + 1); q.push(nn); } } else { pair<int, int> n = make_pair(A, b); if(!vis.count(n)) { vis.insert(n); pair<pair<int, int>, int> nn = make_pair(n, res + 1); q.push(nn); } } } if(b != B) { if((c + b) <= B) { pair<int, int> n = make_pair(a, c + b); if(!vis.count(n)) { vis.insert(n); pair<pair<int, int>, int> nn = make_pair(n, res + 1); q.push(nn); } } else { pair<int, int> n = make_pair(a, B); if(!vis.count(n)) { vis.insert(n); pair<pair<int, int>, int> nn = make_pair(n, res + 1); q.push(nn); } } } } } int main() { cin >> A >> B >> C >> a >> b >> c; int sum = a + b + c; reses.reserve(C + 3); for(int i = 0; i <= C; i++) { reses.push_back(-1); } generate_moves(a, b, c, 0); while(!q.empty()) { pair<pair<int, int>, int> p = q.front(); q.pop(); int aa = p.first.first, bb = p.first.second; int cc = sum - aa - bb; generate_moves(aa, bb, cc, p.second); } for(int i = 0; i <= C; i++) { cout << reses[i] << ' '; } cout << endl; }
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | #include<iostream> #include<unordered_set> #include<queue> #include<utility> using namespace std; int A, B, C, a, b, c; #define ull unsigned long long struct phash { inline size_t operator()(const pair<int, int> &v) const { return v.first * 100003 + v.second; } }; unordered_set< pair<int, int>, phash > vis; queue< pair<pair<int, int>, int> > q; vector<int> reses; void generate_moves(int a, int b, int c, int res) { if(reses[a] == -1) { reses[a] = res; } if(reses[b] == -1) { reses[b] = res; } if(reses[c] == -1) { reses[c] = res; } if(a != 0) { if(b != B) { if((b + a) <= B) { pair<int, int> n = make_pair(0, a + b); if(!vis.count(n)) { vis.insert(n); pair<pair<int, int>, int> nn = make_pair(n, res + 1); q.push(nn); } } else { pair<int, int> n = make_pair(a + b - B, B); if(!vis.count(n)) { vis.insert(n); pair<pair<int, int>, int> nn = make_pair(n, res + 1); q.push(nn); } } } if(c != C) { if((c + a) <= C) { pair<int, int> n = make_pair(0, b); if(!vis.count(n)) { vis.insert(n); pair<pair<int, int>, int> nn = make_pair(n, res + 1); q.push(nn); } } else { pair<int, int> n = make_pair(a + c - C, b); if(!vis.count(n)) { vis.insert(n); pair<pair<int, int>, int> nn = make_pair(n, res + 1); q.push(nn); } } } } if(b != 0) { if(a != A) { if((b + a) <= A) { pair<int, int> n = make_pair(a + b, 0); if(!vis.count(n)) { vis.insert(n); pair<pair<int, int>, int> nn = make_pair(n, res + 1); q.push(nn); } } else { pair<int, int> n = make_pair(A, b + a - A); if(!vis.count(n)) { vis.insert(n); pair<pair<int, int>, int> nn = make_pair(n, res + 1); q.push(nn); } } } if(c != C) { if((c + b) <= C) { pair<int, int> n = make_pair(a, 0); if(!vis.count(n)) { vis.insert(n); pair<pair<int, int>, int> nn = make_pair(n, res + 1); q.push(nn); } } else { pair<int, int> n = make_pair(a, b + c - C); if(!vis.count(n)) { vis.insert(n); pair<pair<int, int>, int> nn = make_pair(n, res + 1); q.push(nn); } } } } if(c != 0) { if(a != A) { if((c + a) <= A) { pair<int, int> n = make_pair(c + a, b); if(!vis.count(n)) { vis.insert(n); pair<pair<int, int>, int> nn = make_pair(n, res + 1); q.push(nn); } } else { pair<int, int> n = make_pair(A, b); if(!vis.count(n)) { vis.insert(n); pair<pair<int, int>, int> nn = make_pair(n, res + 1); q.push(nn); } } } if(b != B) { if((c + b) <= B) { pair<int, int> n = make_pair(a, c + b); if(!vis.count(n)) { vis.insert(n); pair<pair<int, int>, int> nn = make_pair(n, res + 1); q.push(nn); } } else { pair<int, int> n = make_pair(a, B); if(!vis.count(n)) { vis.insert(n); pair<pair<int, int>, int> nn = make_pair(n, res + 1); q.push(nn); } } } } } int main() { cin >> A >> B >> C >> a >> b >> c; int sum = a + b + c; reses.reserve(C + 3); for(int i = 0; i <= C; i++) { reses.push_back(-1); } generate_moves(a, b, c, 0); while(!q.empty()) { pair<pair<int, int>, int> p = q.front(); q.pop(); int aa = p.first.first, bb = p.first.second; int cc = sum - aa - bb; generate_moves(aa, bb, cc, p.second); } for(int i = 0; i <= C; i++) { cout << reses[i] << ' '; } cout << endl; } |