#include <iostream> #include <tuple> #include <vector> #include <unordered_set> namespace { using std::cin; using std::cout; using std::get; using liters_t = size_t; using bottle_contents_t = std::tuple<liters_t, liters_t, liters_t>; using pouring_states_t = std::vector<bottle_contents_t>; using answer_t = std::vector<ssize_t>; size_t const A = 0; size_t const B = 1; size_t const C = 2; ssize_t const NOT_FOUND = -1; struct hash_bottle_contents { size_t operator()(bottle_contents_t const & b) const { return get<A>(b) * 100000 + get<B>(b) * 1000 + get<C>(b); } }; using verified_states_t = std::unordered_set<bottle_contents_t, hash_bottle_contents>; void pour_a2b(liters_t & a, liters_t & b, liters_t const & max_b) { static liters_t tmp; tmp = max_b - b; if (a <= tmp) { b += a; a = 0; } else { a -= tmp; b = max_b; } } } int main() { bottle_contents_t caps; bottle_contents_t bottles; cin >> get<A>(caps); cin >> get<B>(caps); cin >> get<C>(caps); cin >> get<A>(bottles); cin >> get<B>(bottles); cin >> get<C>(bottles); answer_t answer(get<C>(caps) + 1, -1); pouring_states_t pouring_states; pouring_states.push_back(bottles); verified_states_t verified; verified.insert(bottles); liters_t found_liters = 0; ssize_t curr_pouring = 0; size_t curr_id = 0, next_id = 1; while (found_liters <= get<C>(caps) && curr_id < pouring_states.size()) { while (curr_id < next_id) { bottles = pouring_states[curr_id]; if (answer[get<A>(bottles)] == NOT_FOUND) { answer[get<A>(bottles)] = curr_pouring; ++found_liters; } if (answer[get<B>(bottles)] == NOT_FOUND) { answer[get<B>(bottles)] = curr_pouring; ++found_liters; } if (answer[get<C>(bottles)] == NOT_FOUND) { answer[get<C>(bottles)] = curr_pouring; ++found_liters; } pour_a2b(get<A>(bottles), get<B>(bottles), get<B>(caps)); if (verified.find(bottles) == verified.end()) { pouring_states.push_back(bottles); verified.insert(bottles); } bottles = pouring_states[curr_id]; pour_a2b(get<A>(bottles), get<C>(bottles), get<C>(caps)); if (verified.find(bottles) == verified.end()) { pouring_states.push_back(bottles); verified.insert(bottles); } bottles = pouring_states[curr_id]; pour_a2b(get<B>(bottles), get<A>(bottles), get<A>(caps)); if (verified.find(bottles) == verified.end()) { pouring_states.push_back(bottles); verified.insert(bottles); } bottles = pouring_states[curr_id]; pour_a2b(get<B>(bottles), get<C>(bottles), get<C>(caps)); if (verified.find(bottles) == verified.end()) { pouring_states.push_back(bottles); verified.insert(bottles); } bottles = pouring_states[curr_id]; pour_a2b(get<C>(bottles), get<A>(bottles), get<A>(caps)); if (verified.find(bottles) == verified.end()) { pouring_states.push_back(bottles); verified.insert(bottles); } bottles = pouring_states[curr_id]; pour_a2b(get<C>(bottles), get<B>(bottles), get<B>(caps)); if (verified.find(bottles) == verified.end()) { pouring_states.push_back(bottles); verified.insert(bottles); } ++curr_id; } next_id = pouring_states.size(); ++curr_pouring; } for (auto const & ans: answer) cout << ans << ' '; 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 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | #include <iostream> #include <tuple> #include <vector> #include <unordered_set> namespace { using std::cin; using std::cout; using std::get; using liters_t = size_t; using bottle_contents_t = std::tuple<liters_t, liters_t, liters_t>; using pouring_states_t = std::vector<bottle_contents_t>; using answer_t = std::vector<ssize_t>; size_t const A = 0; size_t const B = 1; size_t const C = 2; ssize_t const NOT_FOUND = -1; struct hash_bottle_contents { size_t operator()(bottle_contents_t const & b) const { return get<A>(b) * 100000 + get<B>(b) * 1000 + get<C>(b); } }; using verified_states_t = std::unordered_set<bottle_contents_t, hash_bottle_contents>; void pour_a2b(liters_t & a, liters_t & b, liters_t const & max_b) { static liters_t tmp; tmp = max_b - b; if (a <= tmp) { b += a; a = 0; } else { a -= tmp; b = max_b; } } } int main() { bottle_contents_t caps; bottle_contents_t bottles; cin >> get<A>(caps); cin >> get<B>(caps); cin >> get<C>(caps); cin >> get<A>(bottles); cin >> get<B>(bottles); cin >> get<C>(bottles); answer_t answer(get<C>(caps) + 1, -1); pouring_states_t pouring_states; pouring_states.push_back(bottles); verified_states_t verified; verified.insert(bottles); liters_t found_liters = 0; ssize_t curr_pouring = 0; size_t curr_id = 0, next_id = 1; while (found_liters <= get<C>(caps) && curr_id < pouring_states.size()) { while (curr_id < next_id) { bottles = pouring_states[curr_id]; if (answer[get<A>(bottles)] == NOT_FOUND) { answer[get<A>(bottles)] = curr_pouring; ++found_liters; } if (answer[get<B>(bottles)] == NOT_FOUND) { answer[get<B>(bottles)] = curr_pouring; ++found_liters; } if (answer[get<C>(bottles)] == NOT_FOUND) { answer[get<C>(bottles)] = curr_pouring; ++found_liters; } pour_a2b(get<A>(bottles), get<B>(bottles), get<B>(caps)); if (verified.find(bottles) == verified.end()) { pouring_states.push_back(bottles); verified.insert(bottles); } bottles = pouring_states[curr_id]; pour_a2b(get<A>(bottles), get<C>(bottles), get<C>(caps)); if (verified.find(bottles) == verified.end()) { pouring_states.push_back(bottles); verified.insert(bottles); } bottles = pouring_states[curr_id]; pour_a2b(get<B>(bottles), get<A>(bottles), get<A>(caps)); if (verified.find(bottles) == verified.end()) { pouring_states.push_back(bottles); verified.insert(bottles); } bottles = pouring_states[curr_id]; pour_a2b(get<B>(bottles), get<C>(bottles), get<C>(caps)); if (verified.find(bottles) == verified.end()) { pouring_states.push_back(bottles); verified.insert(bottles); } bottles = pouring_states[curr_id]; pour_a2b(get<C>(bottles), get<A>(bottles), get<A>(caps)); if (verified.find(bottles) == verified.end()) { pouring_states.push_back(bottles); verified.insert(bottles); } bottles = pouring_states[curr_id]; pour_a2b(get<C>(bottles), get<B>(bottles), get<B>(caps)); if (verified.find(bottles) == verified.end()) { pouring_states.push_back(bottles); verified.insert(bottles); } ++curr_id; } next_id = pouring_states.size(); ++curr_pouring; } for (auto const & ans: answer) cout << ans << ' '; return 0; } |