#include <iostream> #include <tuple> #include <map> #include <queue> #include <utility> constexpr int maxVal = 1e5; constexpr int INF = 1e9 + 7; int A, B, C; int a, b, c; std::map<std::tuple<int, int, int>, int> M; std::queue<std::pair<std::tuple<int, int, int>, int>> Q; int _max, temp; int ans[maxVal + 1]; void f(std::tuple<int, int, int> T, int ile) { if(M[T] != 0 && M[T] < ile) { return; } M[T] = ile; a = std::get<0>(T); b = std::get<1>(T); c = std::get<2>(T); ans[a] = std::min(ans[a], ile); ans[b] = std::min(ans[b], ile); ans[c] = std::min(ans[c], ile); // a -> b temp = std::min(a, B - b); Q.push({std::make_tuple(a - temp, b + temp, c), ile + 1}); // a -> c temp = std::min(a, C - c); Q.push({std::make_tuple(a - temp, b, c + temp), ile + 1}); // b -> a temp = std::min(b, A - a); Q.push({std::make_tuple(a + temp, b - temp, c), ile + 1}); // b -> c temp = std::min(b, C - c); Q.push({std::make_tuple(a, b - temp, c + temp), ile + 1}); // c -> a temp = std::min(c, A - a); Q.push({std::make_tuple(a + temp, b, c - temp), ile + 1}); // c -> b temp = std::min(c, B - b); Q.push({std::make_tuple(a, b + temp, c - temp), ile + 1}); } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); std::cin >> A >> B >> C; std::cin >> a >> b >> c; _max = std::max(A, std::max(B, C)); for(int i=0; i<=_max; i++) { ans[i] = INF; } Q.push({std::make_tuple(a, b, c), 0}); while(!Q.empty()) { f(Q.front().first, Q.front().second); Q.pop(); } for(int i=0; i<=_max; i++) { if(ans[i] == INF) { std::cout << -1; } else { std::cout << ans[i]; } std::cout << ' '; } }
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 | #include <iostream> #include <tuple> #include <map> #include <queue> #include <utility> constexpr int maxVal = 1e5; constexpr int INF = 1e9 + 7; int A, B, C; int a, b, c; std::map<std::tuple<int, int, int>, int> M; std::queue<std::pair<std::tuple<int, int, int>, int>> Q; int _max, temp; int ans[maxVal + 1]; void f(std::tuple<int, int, int> T, int ile) { if(M[T] != 0 && M[T] < ile) { return; } M[T] = ile; a = std::get<0>(T); b = std::get<1>(T); c = std::get<2>(T); ans[a] = std::min(ans[a], ile); ans[b] = std::min(ans[b], ile); ans[c] = std::min(ans[c], ile); // a -> b temp = std::min(a, B - b); Q.push({std::make_tuple(a - temp, b + temp, c), ile + 1}); // a -> c temp = std::min(a, C - c); Q.push({std::make_tuple(a - temp, b, c + temp), ile + 1}); // b -> a temp = std::min(b, A - a); Q.push({std::make_tuple(a + temp, b - temp, c), ile + 1}); // b -> c temp = std::min(b, C - c); Q.push({std::make_tuple(a, b - temp, c + temp), ile + 1}); // c -> a temp = std::min(c, A - a); Q.push({std::make_tuple(a + temp, b, c - temp), ile + 1}); // c -> b temp = std::min(c, B - b); Q.push({std::make_tuple(a, b + temp, c - temp), ile + 1}); } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); std::cin >> A >> B >> C; std::cin >> a >> b >> c; _max = std::max(A, std::max(B, C)); for(int i=0; i<=_max; i++) { ans[i] = INF; } Q.push({std::make_tuple(a, b, c), 0}); while(!Q.empty()) { f(Q.front().first, Q.front().second); Q.pop(); } for(int i=0; i<=_max; i++) { if(ans[i] == INF) { std::cout << -1; } else { std::cout << ans[i]; } std::cout << ' '; } } |