#include <iostream> #include <algorithm> #include <map> #include <queue> using namespace std; int p[3], wynik[100001]; tuple<int, int, int> cur; map<tuple<int, int, int>, int> lol; queue<tuple<int, int, int>> q; vector<tuple<int, int, int>> gen_sas(tuple<int, int, int> x) { int u[3]; u[0] = get<0>(x); u[1] = get<1>(x); u[2] = get<2>(x); int w[3]; vector<tuple<int, int, int>> ret; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) if (i != j) { copy_n(u, 3, w); if (w[i] + w[j] <= p[j]) w[j] += w[i], w[i] = 0; else w[i] -= p[j] - w[j], w[j] = p[j]; ret.emplace_back(w[0], w[1], w[2]); } } return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> p[0] >> p[1] >> p[2]; cin >> get<0>(cur) >> get<1>(cur) >> get<2>(cur); lol[cur] = 1; q.push(cur); while (!q.empty()) { tuple<int, int, int> x = q.front(); q.pop(); int dist = lol[x]; if (!wynik[get<0>(x)]) wynik[get<0>(x)] = dist; if (!wynik[get<1>(x)]) wynik[get<1>(x)] = dist; if (!wynik[get<2>(x)]) wynik[get<2>(x)] = dist; vector<tuple<int, int, int>> v = gen_sas(x); for (const auto &c: v) { if (!lol[c]) { lol[c] = dist + 1; q.push(c); } } } for (int i = 0; i <= p[2]; i++) cout << wynik[i] - 1<< ' '; cout << endl; }
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 | #include <iostream> #include <algorithm> #include <map> #include <queue> using namespace std; int p[3], wynik[100001]; tuple<int, int, int> cur; map<tuple<int, int, int>, int> lol; queue<tuple<int, int, int>> q; vector<tuple<int, int, int>> gen_sas(tuple<int, int, int> x) { int u[3]; u[0] = get<0>(x); u[1] = get<1>(x); u[2] = get<2>(x); int w[3]; vector<tuple<int, int, int>> ret; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) if (i != j) { copy_n(u, 3, w); if (w[i] + w[j] <= p[j]) w[j] += w[i], w[i] = 0; else w[i] -= p[j] - w[j], w[j] = p[j]; ret.emplace_back(w[0], w[1], w[2]); } } return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> p[0] >> p[1] >> p[2]; cin >> get<0>(cur) >> get<1>(cur) >> get<2>(cur); lol[cur] = 1; q.push(cur); while (!q.empty()) { tuple<int, int, int> x = q.front(); q.pop(); int dist = lol[x]; if (!wynik[get<0>(x)]) wynik[get<0>(x)] = dist; if (!wynik[get<1>(x)]) wynik[get<1>(x)] = dist; if (!wynik[get<2>(x)]) wynik[get<2>(x)] = dist; vector<tuple<int, int, int>> v = gen_sas(x); for (const auto &c: v) { if (!lol[c]) { lol[c] = dist + 1; q.push(c); } } } for (int i = 0; i <= p[2]; i++) cout << wynik[i] - 1<< ' '; cout << endl; } |