#include <bits/stdc++.h> using namespace std; constexpr int inf = std::numeric_limits<int>::max(); typedef std::array<int, 3> State; State A; vector<int> res; map<State, int> states; deque<State> q; void AddState(int t, State a) { for (int x : a) res[x] = min(res[x], t); q.push_back(a); states[a] = t; } void ReadState(State& s) { for (int& x : s) scanf("%d", &x); } void Transfer(State a, int t, int i, int j) { int s = a[i] + a[j]; if (s <= A[j]) { a[j] = s; a[i] = 0; } else { a[i] = s - A[j]; a[j] = A[j]; } if (states.find(a) == states.end()) AddState(t + 1, a); } int main() { State a; ReadState(A); ReadState(a); res.resize(A[2] + 1, inf); AddState(0, a); while (!q.empty()) { a = q.front(); q.pop_front(); int t = states[a]; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; ++j) if (i != j) Transfer(a, t, i, j); } for (size_t i = 0; i < res.size(); ++i) { if (i) putchar(' '); printf("%d", res[i] == inf ? -1 : res[i]); } printf("\n"); 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 <bits/stdc++.h> using namespace std; constexpr int inf = std::numeric_limits<int>::max(); typedef std::array<int, 3> State; State A; vector<int> res; map<State, int> states; deque<State> q; void AddState(int t, State a) { for (int x : a) res[x] = min(res[x], t); q.push_back(a); states[a] = t; } void ReadState(State& s) { for (int& x : s) scanf("%d", &x); } void Transfer(State a, int t, int i, int j) { int s = a[i] + a[j]; if (s <= A[j]) { a[j] = s; a[i] = 0; } else { a[i] = s - A[j]; a[j] = A[j]; } if (states.find(a) == states.end()) AddState(t + 1, a); } int main() { State a; ReadState(A); ReadState(a); res.resize(A[2] + 1, inf); AddState(0, a); while (!q.empty()) { a = q.front(); q.pop_front(); int t = states[a]; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; ++j) if (i != j) Transfer(a, t, i, j); } for (size_t i = 0; i < res.size(); ++i) { if (i) putchar(' '); printf("%d", res[i] == inf ? -1 : res[i]); } printf("\n"); return 0; } |