#include <iostream> #include <tuple> #include <queue> #include <vector> #include <map> using Triplet = std::tuple<int, int, int>; std::vector<Triplet> reachableStates(const Triplet &jugs, const Triplet &bounds) { int v; std::vector<Triplet> states; v = std::min(std::get<0>(jugs), std::get<1>(bounds) - std::get<1>(jugs)); states.push_back(std::make_tuple(std::get<0>(jugs) - v, std::get<1>(jugs) + v, std::get<2>(jugs))); v = std::min(std::get<0>(jugs), std::get<2>(bounds) - std::get<2>(jugs)); states.push_back(std::make_tuple(std::get<0>(jugs) - v, std::get<1>(jugs), std::get<2>(jugs) + v)); v = std::min(std::get<1>(jugs), std::get<0>(bounds) - std::get<0>(jugs)); states.push_back(std::make_tuple(std::get<0>(jugs) + v, std::get<1>(jugs) - v, std::get<2>(jugs))); v = std::min(std::get<1>(jugs), std::get<2>(bounds) - std::get<2>(jugs)); states.push_back(std::make_tuple(std::get<0>(jugs) , std::get<1>(jugs) - v, std::get<2>(jugs) + v)); v = std::min(std::get<2>(jugs), std::get<0>(bounds) - std::get<0>(jugs)); states.push_back(std::make_tuple(std::get<0>(jugs) + v, std::get<1>(jugs), std::get<2>(jugs) - v)); v = std::min(std::get<2>(jugs), std::get<1>(bounds) - std::get<1>(jugs)); states.push_back(std::make_tuple(std::get<0>(jugs), std::get<1>(jugs) + v, std::get<2>(jugs) - v)); return states; } int main() { int A, B, C; std::cin >> A >> B >> C; auto bounds = std::make_tuple(A, B, C); int a, b, c; std::cin >> a >> b >> c; auto start = std::make_tuple(a, b, c); std::queue<Triplet> q; q.push(start); std::map<Triplet, int> distance = {{start, 0}}; while(!q.empty()) { auto now = q.front(); q.pop(); auto states = reachableStates(now, bounds); for (auto s: states) { if (distance.find(s) == distance.end()) { distance[s] = distance[now] + 1; q.push(s); } } } std::vector<int> result(C + 1, A + B + C); for (const auto &[t, d]: distance) { result[std::get<0>(t)] = std::min(result[std::get<0>(t)], d); result[std::get<1>(t)] = std::min(result[std::get<1>(t)], d); result[std::get<2>(t)] = std::min(result[std::get<2>(t)], d); } for (int i = 0; i <= C; i++) { std::cout << (result[i] == A + B + C ? -1 : result[i]) << " "; } std::cout << '\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 66 67 68 69 70 | #include <iostream> #include <tuple> #include <queue> #include <vector> #include <map> using Triplet = std::tuple<int, int, int>; std::vector<Triplet> reachableStates(const Triplet &jugs, const Triplet &bounds) { int v; std::vector<Triplet> states; v = std::min(std::get<0>(jugs), std::get<1>(bounds) - std::get<1>(jugs)); states.push_back(std::make_tuple(std::get<0>(jugs) - v, std::get<1>(jugs) + v, std::get<2>(jugs))); v = std::min(std::get<0>(jugs), std::get<2>(bounds) - std::get<2>(jugs)); states.push_back(std::make_tuple(std::get<0>(jugs) - v, std::get<1>(jugs), std::get<2>(jugs) + v)); v = std::min(std::get<1>(jugs), std::get<0>(bounds) - std::get<0>(jugs)); states.push_back(std::make_tuple(std::get<0>(jugs) + v, std::get<1>(jugs) - v, std::get<2>(jugs))); v = std::min(std::get<1>(jugs), std::get<2>(bounds) - std::get<2>(jugs)); states.push_back(std::make_tuple(std::get<0>(jugs) , std::get<1>(jugs) - v, std::get<2>(jugs) + v)); v = std::min(std::get<2>(jugs), std::get<0>(bounds) - std::get<0>(jugs)); states.push_back(std::make_tuple(std::get<0>(jugs) + v, std::get<1>(jugs), std::get<2>(jugs) - v)); v = std::min(std::get<2>(jugs), std::get<1>(bounds) - std::get<1>(jugs)); states.push_back(std::make_tuple(std::get<0>(jugs), std::get<1>(jugs) + v, std::get<2>(jugs) - v)); return states; } int main() { int A, B, C; std::cin >> A >> B >> C; auto bounds = std::make_tuple(A, B, C); int a, b, c; std::cin >> a >> b >> c; auto start = std::make_tuple(a, b, c); std::queue<Triplet> q; q.push(start); std::map<Triplet, int> distance = {{start, 0}}; while(!q.empty()) { auto now = q.front(); q.pop(); auto states = reachableStates(now, bounds); for (auto s: states) { if (distance.find(s) == distance.end()) { distance[s] = distance[now] + 1; q.push(s); } } } std::vector<int> result(C + 1, A + B + C); for (const auto &[t, d]: distance) { result[std::get<0>(t)] = std::min(result[std::get<0>(t)], d); result[std::get<1>(t)] = std::min(result[std::get<1>(t)], d); result[std::get<2>(t)] = std::min(result[std::get<2>(t)], d); } for (int i = 0; i <= C; i++) { std::cout << (result[i] == A + B + C ? -1 : result[i]) << " "; } std::cout << '\n'; return 0; } |