#include <iostream> #include <vector> #include <map> #include <queue> using namespace std; const int INF = 1000000000; int main() { ios_base::sync_with_stdio(0); vector<int> mam(3); vector<int> obj(3); cin >> obj[0] >> obj[1] >> obj[2]; cin >> mam[0] >> mam[1] >> mam[2]; queue< vector<int> > Q; map< vector<int>, int > odl; vector<int> odp(100001, INF); Q.push(mam); odl[mam] = 0; while (!Q.empty()) { vector<int> akt = Q.front(); Q.pop(); for (int i = 0; i < 3; ++i) odp[akt[i]] = min(odp[akt[i]], odl[akt]); vector<int> puste(3); for (int i = 0; i < 3; ++i) puste[i] = obj[i] - akt[i]; vector<int> nowy; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { if (i != j) { nowy = akt; if (akt[i] <= puste[j]) { nowy[i] = 0; nowy[j] += akt[i]; } else { nowy[i] -= puste[j]; nowy[j] = obj[j]; } if (odl.find(nowy) == odl.end()) { odl[nowy] = odl[akt] + 1; Q.push(nowy); } } } } } for (int i = 0; i <= obj[2]; ++i) { if (odp[i] == INF) odp[i] = -1; cout << odp[i] << " "; } 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 | #include <iostream> #include <vector> #include <map> #include <queue> using namespace std; const int INF = 1000000000; int main() { ios_base::sync_with_stdio(0); vector<int> mam(3); vector<int> obj(3); cin >> obj[0] >> obj[1] >> obj[2]; cin >> mam[0] >> mam[1] >> mam[2]; queue< vector<int> > Q; map< vector<int>, int > odl; vector<int> odp(100001, INF); Q.push(mam); odl[mam] = 0; while (!Q.empty()) { vector<int> akt = Q.front(); Q.pop(); for (int i = 0; i < 3; ++i) odp[akt[i]] = min(odp[akt[i]], odl[akt]); vector<int> puste(3); for (int i = 0; i < 3; ++i) puste[i] = obj[i] - akt[i]; vector<int> nowy; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { if (i != j) { nowy = akt; if (akt[i] <= puste[j]) { nowy[i] = 0; nowy[j] += akt[i]; } else { nowy[i] -= puste[j]; nowy[j] = obj[j]; } if (odl.find(nowy) == odl.end()) { odl[nowy] = odl[akt] + 1; Q.push(nowy); } } } } } for (int i = 0; i <= obj[2]; ++i) { if (odp[i] == INF) odp[i] = -1; cout << odp[i] << " "; } return 0; } |