#include <iostream> #include <vector> #include <algorithm> #include <deque> #include <climits> using namespace std; int main() { std::ios::sync_with_stdio(false); int A, B, C, a, b, c, s; cin >> A >> B >> C >> a >> b >> c; vector<int> minimumMoves(100001, INT_MAX); deque<vector<int>> initialStatesDeque; deque<vector<int>> initialStates; initialStatesDeque.push_back({ a,b,c,0 }); while (initialStates.size() < 43) { vector<int> state = initialStatesDeque.front(); initialStates.push_back(state); initialStatesDeque.pop_front(); // update minimum moves minimumMoves[state[0]] = min(minimumMoves[state[0]], state[3]); minimumMoves[state[1]] = min(minimumMoves[state[1]], state[3]); minimumMoves[state[2]] = min(minimumMoves[state[2]], state[3]); // A <-> B s = state[0] + state[1]; initialStatesDeque.push_back({ max(s - B, 0), min(s, B), state[2], state[3] + 1 }); initialStatesDeque.push_back({ min(s, A), max(s - A, 0), state[2], state[3] + 1 }); // A <-> C s = state[0] + state[2]; initialStatesDeque.push_back({ max(s - C, 0), state[1], min(s, C), state[3] + 1 }); initialStatesDeque.push_back({ min(s, A), state[1], max(s - A, 0), state[3] + 1 }); // B <-> C s = state[1] + state[2]; initialStatesDeque.push_back({ state[0], max(s - C, 0), min(s, C), state[3] + 1 }); initialStatesDeque.push_back({ state[0], min(s, B), max(s - B, 0), state[3] + 1 }); } // B -> A -> C for (int i = 0; i < initialStates.size(); i++) { vector<int> state = initialStates[i]; while (state[1] > 0 && state[2] < C) { // B -> A s = state[0] + state[1]; state = { min(s, A), max(s - A, 0), state[2], state[3] + 1 }; minimumMoves[state[0]] = min(minimumMoves[state[0]], state[3]); minimumMoves[state[1]] = min(minimumMoves[state[1]], state[3]); // A -> C s = state[0] + state[2]; state = { max(s - C, 0), state[1], min(s, C), state[3] + 1 }; minimumMoves[state[0]] = min(minimumMoves[state[0]], state[3]); minimumMoves[state[2]] = min(minimumMoves[state[2]], state[3]); } } // C -> A -> B for (int i = 0; i < initialStates.size(); i++) { vector<int> state = initialStates[i]; while (state[1] < B && state[2] > 0) { // C -> A s = state[0] + state[2]; state = { min(s, A), state[1], max(s - A, 0), state[3] + 1 }; minimumMoves[state[0]] = min(minimumMoves[state[0]], state[3]); minimumMoves[state[2]] = min(minimumMoves[state[2]], state[3]); // A -> B s = state[0] + state[1]; state = { max(s - B, 0), min(s, B), state[2], state[3] + 1 }; minimumMoves[state[0]] = min(minimumMoves[state[0]], state[3]); minimumMoves[state[1]] = min(minimumMoves[state[1]], state[3]); } } for (int i = 0; i <= C; i++) { if (minimumMoves[i] == INT_MAX) { minimumMoves[i] = -1; } } for (int i = 0; i <= C; i++) { cout << minimumMoves[i] << " "; } }
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 | #include <iostream> #include <vector> #include <algorithm> #include <deque> #include <climits> using namespace std; int main() { std::ios::sync_with_stdio(false); int A, B, C, a, b, c, s; cin >> A >> B >> C >> a >> b >> c; vector<int> minimumMoves(100001, INT_MAX); deque<vector<int>> initialStatesDeque; deque<vector<int>> initialStates; initialStatesDeque.push_back({ a,b,c,0 }); while (initialStates.size() < 43) { vector<int> state = initialStatesDeque.front(); initialStates.push_back(state); initialStatesDeque.pop_front(); // update minimum moves minimumMoves[state[0]] = min(minimumMoves[state[0]], state[3]); minimumMoves[state[1]] = min(minimumMoves[state[1]], state[3]); minimumMoves[state[2]] = min(minimumMoves[state[2]], state[3]); // A <-> B s = state[0] + state[1]; initialStatesDeque.push_back({ max(s - B, 0), min(s, B), state[2], state[3] + 1 }); initialStatesDeque.push_back({ min(s, A), max(s - A, 0), state[2], state[3] + 1 }); // A <-> C s = state[0] + state[2]; initialStatesDeque.push_back({ max(s - C, 0), state[1], min(s, C), state[3] + 1 }); initialStatesDeque.push_back({ min(s, A), state[1], max(s - A, 0), state[3] + 1 }); // B <-> C s = state[1] + state[2]; initialStatesDeque.push_back({ state[0], max(s - C, 0), min(s, C), state[3] + 1 }); initialStatesDeque.push_back({ state[0], min(s, B), max(s - B, 0), state[3] + 1 }); } // B -> A -> C for (int i = 0; i < initialStates.size(); i++) { vector<int> state = initialStates[i]; while (state[1] > 0 && state[2] < C) { // B -> A s = state[0] + state[1]; state = { min(s, A), max(s - A, 0), state[2], state[3] + 1 }; minimumMoves[state[0]] = min(minimumMoves[state[0]], state[3]); minimumMoves[state[1]] = min(minimumMoves[state[1]], state[3]); // A -> C s = state[0] + state[2]; state = { max(s - C, 0), state[1], min(s, C), state[3] + 1 }; minimumMoves[state[0]] = min(minimumMoves[state[0]], state[3]); minimumMoves[state[2]] = min(minimumMoves[state[2]], state[3]); } } // C -> A -> B for (int i = 0; i < initialStates.size(); i++) { vector<int> state = initialStates[i]; while (state[1] < B && state[2] > 0) { // C -> A s = state[0] + state[2]; state = { min(s, A), state[1], max(s - A, 0), state[3] + 1 }; minimumMoves[state[0]] = min(minimumMoves[state[0]], state[3]); minimumMoves[state[2]] = min(minimumMoves[state[2]], state[3]); // A -> B s = state[0] + state[1]; state = { max(s - B, 0), min(s, B), state[2], state[3] + 1 }; minimumMoves[state[0]] = min(minimumMoves[state[0]], state[3]); minimumMoves[state[1]] = min(minimumMoves[state[1]], state[3]); } } for (int i = 0; i <= C; i++) { if (minimumMoves[i] == INT_MAX) { minimumMoves[i] = -1; } } for (int i = 0; i <= C; i++) { cout << minimumMoves[i] << " "; } } |