#include <iostream> #include <algorithm> #include <queue> #include <map> using namespace std; int but[3]; int roz[3]; int ans[100007]; int a, b, c, A, B, C, val = 0; queue <pair<int, pair<int, pair <int, int>>>> q; map <vector <int>, bool > m; bool znajdz(vector<int> data) { if (m.find(data) != m.end()) return true; else return false; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> A >> B >> C; roz[0] = A; roz[1] = B; roz[2] = C; for (int i = 0; i <= C; i++) ans[i] = -1; cin >> a >> b >> c; ans[a] = 0; ans[b] = 0; ans[c] = 0; q.push({ val, {a, {b,c}} }); while (!q.empty()) { //cout << q.front().first << endl; a = q.front().second.first; b = q.front().second.second.first; c = q.front().second.second.second; vector<int> szuk{ a, b, c }; if (znajdz(szuk)) { q.pop(); } else { m[szuk] = 1; //a>b if (a + b > B) { if (ans[B] == -1) ans[B] = q.front().first + 1;; if (ans[a - (B - b)] == -1) ans[a - (B - b)] = q.front().first + 1; q.push({ q.front().first + 1, {a - (B - b), {B, c}}}); } else if (a + b == B) { if (ans[B] == -1) ans[B] = q.front().first + 1; if (ans[0] == -1) ans[0] = q.front().first + 1; if (q.front().first != 7) q.push({ q.front().first + 1, {0, {B, c}}}); } else if(a + b < B) { if (ans[a + b] == -1) ans[a + b] = q.front().first + 1; if (ans[0] == -1) ans[0] = q.front().first + 1; q.push({ q.front().first + 1, {0, {a + b, c}}}); } //b>a if (a + b > A) { if (ans[A] == -1) ans[A] = q.front().first + 1;; if (ans[b - (A - a)] == -1) ans[b - (A - a)] = q.front().first + 1; q.push({ q.front().first + 1, {A, {b - (A - a), c}}}); } else if (a + b == A) { if (ans[A] == -1) ans[A] = q.front().first + 1; if (ans[0] == -1) ans[0] = q.front().first + 1; q.push({ q.front().first + 1, {A, {0, c}}}); } else if (a + b < A) { if (ans[a + b] == -1) ans[a + b] = q.front().first + 1; if (ans[0] == -1) ans[0] = q.front().first + 1; q.push({ q.front().first + 1, {a + b, {0, c}}}); } //a>c if (a + c > C) { if (ans[C] == -1) ans[C] = q.front().first + 1;; if (ans[a - (C - c)] == -1) ans[a - (C - c)] = q.front().first + 1; q.push({ q.front().first + 1, {a - (C - c), {b, C}} }); } else if (a + c == C) { if (ans[C] == -1) ans[C] = q.front().first + 1; if (ans[0] == -1) ans[0] = q.front().first + 1; q.push({ q.front().first + 1, {0, {b, C}} }); } else if (a + c < C) { if (ans[a + c] == -1) ans[a + c] = q.front().first + 1; if (ans[0] == -1) ans[0] = q.front().first + 1; q.push({ q.front().first + 1, {0, {b, a+c}} }); } //c>a if (a + c > A) { if (ans[A] == -1) ans[A] = q.front().first + 1;; if (ans[c - (A - a)] == -1) ans[c - (A - a)] = q.front().first + 1; q.push({ q.front().first + 1, {A, {b , c - (A - a)}} }); } else if (a + c == A) { if (ans[A] == -1) ans[A] = q.front().first + 1; if (ans[0] == -1) ans[0] = q.front().first + 1; q.push({ q.front().first + 1, {A, {b, 0}} }); } else if (a + c < A) { if (ans[a + c] == -1) ans[a + c] = q.front().first + 1; if (ans[0] == -1) ans[0] = q.front().first + 1; q.push({ q.front().first + 1, {a + c, {b, 0}} }); } //b>c if (b + c > C) { if (ans[C] == -1) ans[C] = q.front().first + 1;; if (ans[b - (C - c)] == -1) ans[b - (C - c)] = q.front().first + 1; q.push({ q.front().first + 1, {a, {b - (C - c), C}} }); } else if (b + c == C) { if (ans[C] == -1) ans[C] = q.front().first + 1; if (ans[0] == -1) ans[0] = q.front().first + 1; q.push({ q.front().first + 1, {a, {0, C}} }); } else if (b + c < C) { if (ans[b+c] == -1) ans[b+c] = q.front().first + 1; if (ans[0] == -1) ans[0] = q.front().first + 1; q.push({ q.front().first + 1, {a, {0, b+c}} }); } //c>b if (b+c > B) { if (ans[B] == -1) ans[B] = q.front().first + 1;; if (ans[c - (B - b)] == -1) ans[c - (B - b)] = q.front().first + 1; q.push({ q.front().first + 1, {a, {B, c - (B - b)}} }); } else if (b + c == B) { if (ans[B] == -1) ans[B] = q.front().first + 1; if (ans[0] == -1) ans[0] = q.front().first + 1; q.push({ q.front().first + 1, {a, {B, 0}} }); } else if (b + c < B) { if (ans[b + c] == -1) ans[b + c] = q.front().first + 1; if (ans[0] == -1) ans[0] = q.front().first + 1; q.push({ q.front().first + 1, {a, {b+c, 0}} }); } q.pop(); } } for (int i = 0; i <= C; i++) { cout << ans[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 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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | #include <iostream> #include <algorithm> #include <queue> #include <map> using namespace std; int but[3]; int roz[3]; int ans[100007]; int a, b, c, A, B, C, val = 0; queue <pair<int, pair<int, pair <int, int>>>> q; map <vector <int>, bool > m; bool znajdz(vector<int> data) { if (m.find(data) != m.end()) return true; else return false; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> A >> B >> C; roz[0] = A; roz[1] = B; roz[2] = C; for (int i = 0; i <= C; i++) ans[i] = -1; cin >> a >> b >> c; ans[a] = 0; ans[b] = 0; ans[c] = 0; q.push({ val, {a, {b,c}} }); while (!q.empty()) { //cout << q.front().first << endl; a = q.front().second.first; b = q.front().second.second.first; c = q.front().second.second.second; vector<int> szuk{ a, b, c }; if (znajdz(szuk)) { q.pop(); } else { m[szuk] = 1; //a>b if (a + b > B) { if (ans[B] == -1) ans[B] = q.front().first + 1;; if (ans[a - (B - b)] == -1) ans[a - (B - b)] = q.front().first + 1; q.push({ q.front().first + 1, {a - (B - b), {B, c}}}); } else if (a + b == B) { if (ans[B] == -1) ans[B] = q.front().first + 1; if (ans[0] == -1) ans[0] = q.front().first + 1; if (q.front().first != 7) q.push({ q.front().first + 1, {0, {B, c}}}); } else if(a + b < B) { if (ans[a + b] == -1) ans[a + b] = q.front().first + 1; if (ans[0] == -1) ans[0] = q.front().first + 1; q.push({ q.front().first + 1, {0, {a + b, c}}}); } //b>a if (a + b > A) { if (ans[A] == -1) ans[A] = q.front().first + 1;; if (ans[b - (A - a)] == -1) ans[b - (A - a)] = q.front().first + 1; q.push({ q.front().first + 1, {A, {b - (A - a), c}}}); } else if (a + b == A) { if (ans[A] == -1) ans[A] = q.front().first + 1; if (ans[0] == -1) ans[0] = q.front().first + 1; q.push({ q.front().first + 1, {A, {0, c}}}); } else if (a + b < A) { if (ans[a + b] == -1) ans[a + b] = q.front().first + 1; if (ans[0] == -1) ans[0] = q.front().first + 1; q.push({ q.front().first + 1, {a + b, {0, c}}}); } //a>c if (a + c > C) { if (ans[C] == -1) ans[C] = q.front().first + 1;; if (ans[a - (C - c)] == -1) ans[a - (C - c)] = q.front().first + 1; q.push({ q.front().first + 1, {a - (C - c), {b, C}} }); } else if (a + c == C) { if (ans[C] == -1) ans[C] = q.front().first + 1; if (ans[0] == -1) ans[0] = q.front().first + 1; q.push({ q.front().first + 1, {0, {b, C}} }); } else if (a + c < C) { if (ans[a + c] == -1) ans[a + c] = q.front().first + 1; if (ans[0] == -1) ans[0] = q.front().first + 1; q.push({ q.front().first + 1, {0, {b, a+c}} }); } //c>a if (a + c > A) { if (ans[A] == -1) ans[A] = q.front().first + 1;; if (ans[c - (A - a)] == -1) ans[c - (A - a)] = q.front().first + 1; q.push({ q.front().first + 1, {A, {b , c - (A - a)}} }); } else if (a + c == A) { if (ans[A] == -1) ans[A] = q.front().first + 1; if (ans[0] == -1) ans[0] = q.front().first + 1; q.push({ q.front().first + 1, {A, {b, 0}} }); } else if (a + c < A) { if (ans[a + c] == -1) ans[a + c] = q.front().first + 1; if (ans[0] == -1) ans[0] = q.front().first + 1; q.push({ q.front().first + 1, {a + c, {b, 0}} }); } //b>c if (b + c > C) { if (ans[C] == -1) ans[C] = q.front().first + 1;; if (ans[b - (C - c)] == -1) ans[b - (C - c)] = q.front().first + 1; q.push({ q.front().first + 1, {a, {b - (C - c), C}} }); } else if (b + c == C) { if (ans[C] == -1) ans[C] = q.front().first + 1; if (ans[0] == -1) ans[0] = q.front().first + 1; q.push({ q.front().first + 1, {a, {0, C}} }); } else if (b + c < C) { if (ans[b+c] == -1) ans[b+c] = q.front().first + 1; if (ans[0] == -1) ans[0] = q.front().first + 1; q.push({ q.front().first + 1, {a, {0, b+c}} }); } //c>b if (b+c > B) { if (ans[B] == -1) ans[B] = q.front().first + 1;; if (ans[c - (B - b)] == -1) ans[c - (B - b)] = q.front().first + 1; q.push({ q.front().first + 1, {a, {B, c - (B - b)}} }); } else if (b + c == B) { if (ans[B] == -1) ans[B] = q.front().first + 1; if (ans[0] == -1) ans[0] = q.front().first + 1; q.push({ q.front().first + 1, {a, {B, 0}} }); } else if (b + c < B) { if (ans[b + c] == -1) ans[b + c] = q.front().first + 1; if (ans[0] == -1) ans[0] = q.front().first + 1; q.push({ q.front().first + 1, {a, {b+c, 0}} }); } q.pop(); } } for (int i = 0; i <= C; i++) { cout << ans[i] << " "; } return 0; } |