#include <bits/stdc++.h> using namespace std; struct hashArray { size_t operator()(const array<int, 3>& a) const { size_t hsh = 1337; for (int e : a) { hsh ^= hash<int>{}(e) + 0x9e3779b9 + (hsh << 6) + (hsh >> 2); } return hsh; } }; unordered_map<array<int, 3>, int, hashArray> v; queue<array<int, 3>> q; int ile[100002]; int A, B, C; int V[3]; int a, b, c; void bfsuj() { while (!q.empty()) { auto node = q.front(); // for (auto elem : node) { // cout << elem << " "; // } // cout << "______" << endl; int score = v[node]; q.pop(); for (auto val : node) { if (ile[val] == -1) { ile[val] = score; } } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (i == j) continue; int vol = min(node[i], V[j] - node[j]); auto temp = node; temp[i] = node[i] - vol; temp[j] = node[j] + vol; if (v.find(temp) == v.end()) { v[temp] = score + 1; q.push(temp); } } } } } int main() { cin >> A >> B >> C; V[0] = A; V[1] = B; V[2] = C; cin >> a >> b >> c; for (int i = 0; i <= C; i++) { ile[i] = -1; } v[{a, b, c}] = 0; q.push({a, b, c}); bfsuj(); for (int i = 0; i < C; i++) { cout << ile[i] << " "; } cout << ile[C] << endl; return 0; }
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 | #include <bits/stdc++.h> using namespace std; struct hashArray { size_t operator()(const array<int, 3>& a) const { size_t hsh = 1337; for (int e : a) { hsh ^= hash<int>{}(e) + 0x9e3779b9 + (hsh << 6) + (hsh >> 2); } return hsh; } }; unordered_map<array<int, 3>, int, hashArray> v; queue<array<int, 3>> q; int ile[100002]; int A, B, C; int V[3]; int a, b, c; void bfsuj() { while (!q.empty()) { auto node = q.front(); // for (auto elem : node) { // cout << elem << " "; // } // cout << "______" << endl; int score = v[node]; q.pop(); for (auto val : node) { if (ile[val] == -1) { ile[val] = score; } } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (i == j) continue; int vol = min(node[i], V[j] - node[j]); auto temp = node; temp[i] = node[i] - vol; temp[j] = node[j] + vol; if (v.find(temp) == v.end()) { v[temp] = score + 1; q.push(temp); } } } } } int main() { cin >> A >> B >> C; V[0] = A; V[1] = B; V[2] = C; cin >> a >> b >> c; for (int i = 0; i <= C; i++) { ile[i] = -1; } v[{a, b, c}] = 0; q.push({a, b, c}); bfsuj(); for (int i = 0; i < C; i++) { cout << ile[i] << " "; } cout << ile[C] << endl; return 0; } |