#include <bits/stdc++.h> using namespace std; const int INF = 1 << 30; int main() { ios_base::sync_with_stdio(false); vector <int> mx(3), value(3); for (int i = 0; i < 3; i++) cin >> mx[i]; for (int i = 0; i < 3; i++) cin >> value[i]; map <vector<int>,int> minMoves; queue <vector<int>> q; q.push(value); minMoves[value] = 0; while (!q.empty()) { auto state = q.front(); q.pop(); for (int i = 0; i < 3; i++) if (state[i] > 0) { for (int j = 0; j < 3; j++) if (state[j] < mx[j] && i != j) { int amountMoved = min(state[i], mx[j] - state[j]); auto newState = state; newState[i] -= amountMoved; newState[j] += amountMoved; if (!minMoves.count(newState)) { minMoves[newState] = 1 + minMoves[state]; q.push(newState); } } } } vector <int> ans(mx[2] + 1, INF); for (auto &e : minMoves) for (int i = 0; i < 3; i++) { int amount = e.first[i]; ans[amount] = min(ans[amount], e.second); } for (int x : ans) { cout << (x == INF ? -1 : x) << ' '; } 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 | #include <bits/stdc++.h> using namespace std; const int INF = 1 << 30; int main() { ios_base::sync_with_stdio(false); vector <int> mx(3), value(3); for (int i = 0; i < 3; i++) cin >> mx[i]; for (int i = 0; i < 3; i++) cin >> value[i]; map <vector<int>,int> minMoves; queue <vector<int>> q; q.push(value); minMoves[value] = 0; while (!q.empty()) { auto state = q.front(); q.pop(); for (int i = 0; i < 3; i++) if (state[i] > 0) { for (int j = 0; j < 3; j++) if (state[j] < mx[j] && i != j) { int amountMoved = min(state[i], mx[j] - state[j]); auto newState = state; newState[i] -= amountMoved; newState[j] += amountMoved; if (!minMoves.count(newState)) { minMoves[newState] = 1 + minMoves[state]; q.push(newState); } } } } vector <int> ans(mx[2] + 1, INF); for (auto &e : minMoves) for (int i = 0; i < 3; i++) { int amount = e.first[i]; ans[amount] = min(ans[amount], e.second); } for (int x : ans) { cout << (x == INF ? -1 : x) << ' '; } return 0; } |