#include <bits/stdc++.h> #define int long long using namespace std; vector<int> caps; map<int, int> res_map; typedef vector<int> state; set<state> visited_states; state pour(const state &old, std::pair<int, int> from_to) { int poured = min(old[from_to.first], caps[from_to.second] - old[from_to.second]); state new_state = old; new_state[from_to.first] -= poured; new_state[from_to.second] += poured; return new_state; } set<state> compute_states(const set<state> &old) { set<state> res; for (auto &s : old) { for (std::pair<int, int> moves : vector<pair<int, int>>{ {0, 1}, {0, 2}, {1, 0}, {1, 2}, {2, 0}, {2, 1}}) { state new_s = pour(s, moves); if (visited_states.count(new_s) == 0) { visited_states.insert(new_s); res.insert(new_s); } } } return res; } signed main() { int cap_a, cap_b, cap_c; int act_a, act_b, act_c; cin >> cap_a >> cap_b >> cap_c; cin >> act_a >> act_b >> act_c; set<state> states; states.insert({act_a, act_b, act_c}); caps.push_back(cap_a); caps.push_back(cap_b); caps.push_back(cap_c); res_map[act_a] = res_map[act_b] = res_map[act_c] = 0; int moves = 0; while (states.size() > 0) { states = compute_states(states); moves++; for (auto &s : states) { if (res_map.count(s[0]) == 0) { res_map[s[0]] = moves; } if (res_map.count(s[1]) == 0) { res_map[s[1]] = moves; } if (res_map.count(s[2]) == 0) { res_map[s[2]] = moves; } } } for (int i = 0; i <= caps[2]; i++) { if (res_map.count(i) == 0) { cout << -1 << " "; } else { cout << res_map[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 | #include <bits/stdc++.h> #define int long long using namespace std; vector<int> caps; map<int, int> res_map; typedef vector<int> state; set<state> visited_states; state pour(const state &old, std::pair<int, int> from_to) { int poured = min(old[from_to.first], caps[from_to.second] - old[from_to.second]); state new_state = old; new_state[from_to.first] -= poured; new_state[from_to.second] += poured; return new_state; } set<state> compute_states(const set<state> &old) { set<state> res; for (auto &s : old) { for (std::pair<int, int> moves : vector<pair<int, int>>{ {0, 1}, {0, 2}, {1, 0}, {1, 2}, {2, 0}, {2, 1}}) { state new_s = pour(s, moves); if (visited_states.count(new_s) == 0) { visited_states.insert(new_s); res.insert(new_s); } } } return res; } signed main() { int cap_a, cap_b, cap_c; int act_a, act_b, act_c; cin >> cap_a >> cap_b >> cap_c; cin >> act_a >> act_b >> act_c; set<state> states; states.insert({act_a, act_b, act_c}); caps.push_back(cap_a); caps.push_back(cap_b); caps.push_back(cap_c); res_map[act_a] = res_map[act_b] = res_map[act_c] = 0; int moves = 0; while (states.size() > 0) { states = compute_states(states); moves++; for (auto &s : states) { if (res_map.count(s[0]) == 0) { res_map[s[0]] = moves; } if (res_map.count(s[1]) == 0) { res_map[s[1]] = moves; } if (res_map.count(s[2]) == 0) { res_map[s[2]] = moves; } } } for (int i = 0; i <= caps[2]; i++) { if (res_map.count(i) == 0) { cout << -1 << " "; } else { cout << res_map[i] << " "; } } } |