#include <bits/stdc++.h> #include <ostream> using namespace std; int found[100001]; struct State { int a, b, c; bool operator==(const State& rhs) const { return a == rhs.a && b == rhs.b && c == rhs.c; } friend ostream &operator<<(ostream &os, const State &state) { //os << "a: " << state.a << " b: " << state.b << " c: " << state.c; os << "(" << state.a << "," << state.b << "," << state.c << ")"; return os; } }; struct Hasher { size_t operator()(const State &a) const { size_t hash = a.a * 1009; hash += a.b; hash *= 1009; hash += a.c; return hash; } }; int main() { unordered_map<State, int, Hasher> zb; queue<pair<State, int>> q; int A, B, C; State begin; cin >> A >> B >> C >> begin.a >> begin.b >> begin.c; zb[begin] = 0; q.push({begin, 0}); while(!q.empty()) { State sp = q.front().first; int prio = q.front().second; q.pop(); //cout << "NS: " << sp << endl; State s = sp; s.b += s.a; s.a = max(0, s.b - B); s.b = min(s.b, B); if(zb.find(s) == zb.end() || zb[s] > prio + 1) { zb[s] = prio + 1; q.push({s, prio + 1}); } s = sp; s.c += s.a; s.a = max(0, s.c - C); s.c = min(s.c, C); if(zb.find(s) == zb.end() || zb[s] > prio + 1) { zb[s] = prio + 1; q.push({s, prio + 1}); } s = sp; s.a += s.b; s.b = max(0, s.a - A); s.a = min(s.a, A); if(zb.find(s) == zb.end() || zb[s] > prio + 1) { zb[s] = prio + 1; q.push({s, prio + 1}); } s = sp; s.a += s.c; s.c = max(0, s.a - A); s.a = min(s.a, A); if(zb.find(s) == zb.end() || zb[s] > prio + 1) { zb[s] = prio + 1; q.push({s, prio + 1}); } s = sp; s.c += s.a; s.a = max(0, s.c - C); s.c = min(s.c, C); if(zb.find(s) == zb.end() || zb[s] > prio + 1) { zb[s] = prio + 1; q.push({s, prio + 1}); } s = sp; s.c += s.b; s.b = max(0, s.c - C); s.c = min(s.c, C); if(zb.find(s) == zb.end() || zb[s] > prio + 1) { zb[s] = prio + 1; q.push({s, prio + 1}); } } const int MX = 100000; fill(found, found + C + 1, MX); for(auto &i: zb) { //cout << i.second << ": " << i.first << endl; found[i.first.a] = min(found[i.first.a], i.second); found[i.first.b] = min(found[i.first.b], i.second); found[i.first.c] = min(found[i.first.c], i.second); } for(int i = 0; i <= C; i++) { if(found[i] == MX) printf("-1 "); else printf("%d ", found[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 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 | #include <bits/stdc++.h> #include <ostream> using namespace std; int found[100001]; struct State { int a, b, c; bool operator==(const State& rhs) const { return a == rhs.a && b == rhs.b && c == rhs.c; } friend ostream &operator<<(ostream &os, const State &state) { //os << "a: " << state.a << " b: " << state.b << " c: " << state.c; os << "(" << state.a << "," << state.b << "," << state.c << ")"; return os; } }; struct Hasher { size_t operator()(const State &a) const { size_t hash = a.a * 1009; hash += a.b; hash *= 1009; hash += a.c; return hash; } }; int main() { unordered_map<State, int, Hasher> zb; queue<pair<State, int>> q; int A, B, C; State begin; cin >> A >> B >> C >> begin.a >> begin.b >> begin.c; zb[begin] = 0; q.push({begin, 0}); while(!q.empty()) { State sp = q.front().first; int prio = q.front().second; q.pop(); //cout << "NS: " << sp << endl; State s = sp; s.b += s.a; s.a = max(0, s.b - B); s.b = min(s.b, B); if(zb.find(s) == zb.end() || zb[s] > prio + 1) { zb[s] = prio + 1; q.push({s, prio + 1}); } s = sp; s.c += s.a; s.a = max(0, s.c - C); s.c = min(s.c, C); if(zb.find(s) == zb.end() || zb[s] > prio + 1) { zb[s] = prio + 1; q.push({s, prio + 1}); } s = sp; s.a += s.b; s.b = max(0, s.a - A); s.a = min(s.a, A); if(zb.find(s) == zb.end() || zb[s] > prio + 1) { zb[s] = prio + 1; q.push({s, prio + 1}); } s = sp; s.a += s.c; s.c = max(0, s.a - A); s.a = min(s.a, A); if(zb.find(s) == zb.end() || zb[s] > prio + 1) { zb[s] = prio + 1; q.push({s, prio + 1}); } s = sp; s.c += s.a; s.a = max(0, s.c - C); s.c = min(s.c, C); if(zb.find(s) == zb.end() || zb[s] > prio + 1) { zb[s] = prio + 1; q.push({s, prio + 1}); } s = sp; s.c += s.b; s.b = max(0, s.c - C); s.c = min(s.c, C); if(zb.find(s) == zb.end() || zb[s] > prio + 1) { zb[s] = prio + 1; q.push({s, prio + 1}); } } const int MX = 100000; fill(found, found + C + 1, MX); for(auto &i: zb) { //cout << i.second << ": " << i.first << endl; found[i.first.a] = min(found[i.first.a], i.second); found[i.first.b] = min(found[i.first.b], i.second); found[i.first.c] = min(found[i.first.c], i.second); } for(int i = 0; i <= C; i++) { if(found[i] == MX) printf("-1 "); else printf("%d ", found[i]); } return 0; } |