#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 1; int n, t; int p[3]; int num; int res[N]; queue<pair<int, pair<int, int>>>q; map<pair<int, pair<int, int>>, int>prz; pair<int, pair<int, int>> key(vector<int>o) { return make_pair(o[0], make_pair(o[1], o[2])); } vector<int> from_key(pair<int, pair<int, int>> k) { return {k.first, k.second.first, k.second.second}; } vector<int>przelane(vector<int>o, int z, int d, int pr) { int ilo = min(o[z], p[d] - o[d]); o[z] -= ilo; o[d] += ilo; if (res[o[z]] == -1 || res[o[z]] > pr) { res[o[z]] = pr; } if (res[o[d]] == -1 || res[o[d]] > pr) { res[o[d]] = pr; } return o; } void f(vector<int>o) { q.push(key(o)); prz[key(o)] = 0; while(!q.empty()) { o = from_key(q.front()); q.pop(); int pr = prz[key(o)]; for (int z = 0; z < 3; z++) { for (int d = 0; d < 3; d++) { if (z != d) { int ilo = min(o[z], p[d] - o[d]); if (ilo > 0) { vector<int>new_o = przelane(o, z, d, pr + 1); auto k = key(new_o); if (prz.find(k) == prz.end()) { prz[k] = pr + 1; q.push(k); } } } } } } } int main() { ios_base::sync_with_stdio(0); for(int i = 0; i < 3; i++) { cin >> p[i]; } vector<int>o; int x; for(int i = 0; i < 3; i++) { cin >> x; o.push_back(x); } for (int i = 0; i <= p[2]; i++) { res[i] = -1; } for(int i = 0; i < 3; i++) { res[o[i]] = 0; } f(o); for (int i = 0; i <= p[2]; i++) { cout << res[i] << " "; } cout << 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 1; int n, t; int p[3]; int num; int res[N]; queue<pair<int, pair<int, int>>>q; map<pair<int, pair<int, int>>, int>prz; pair<int, pair<int, int>> key(vector<int>o) { return make_pair(o[0], make_pair(o[1], o[2])); } vector<int> from_key(pair<int, pair<int, int>> k) { return {k.first, k.second.first, k.second.second}; } vector<int>przelane(vector<int>o, int z, int d, int pr) { int ilo = min(o[z], p[d] - o[d]); o[z] -= ilo; o[d] += ilo; if (res[o[z]] == -1 || res[o[z]] > pr) { res[o[z]] = pr; } if (res[o[d]] == -1 || res[o[d]] > pr) { res[o[d]] = pr; } return o; } void f(vector<int>o) { q.push(key(o)); prz[key(o)] = 0; while(!q.empty()) { o = from_key(q.front()); q.pop(); int pr = prz[key(o)]; for (int z = 0; z < 3; z++) { for (int d = 0; d < 3; d++) { if (z != d) { int ilo = min(o[z], p[d] - o[d]); if (ilo > 0) { vector<int>new_o = przelane(o, z, d, pr + 1); auto k = key(new_o); if (prz.find(k) == prz.end()) { prz[k] = pr + 1; q.push(k); } } } } } } } int main() { ios_base::sync_with_stdio(0); for(int i = 0; i < 3; i++) { cin >> p[i]; } vector<int>o; int x; for(int i = 0; i < 3; i++) { cin >> x; o.push_back(x); } for (int i = 0; i <= p[2]; i++) { res[i] = -1; } for(int i = 0; i < 3; i++) { res[o[i]] = 0; } f(o); for (int i = 0; i <= p[2]; i++) { cout << res[i] << " "; } cout << endl; return 0; } |