#include <iostream> #include <queue> #include <map> #include <tuple> #include <limits> using namespace std; struct syt { int a, b, c, depth; syt(int _a, int _b, int _c, int _d) : a(_a), b(_b), c(_c), depth(_d) {} }; int main(int argc, char **argv) { int a, b, c; int capacity[3]; std::cin >> capacity[0] >> capacity[1] >> capacity[2] >> a >> b >> c; int movnum[capacity[2]+1]; for(int i=0;i<=capacity[2];i++) movnum[i] = std::numeric_limits<int>::max(); movnum[a] = 0; movnum[b] = 0; movnum[c] = 0; map<tuple<int, int, int>, int> m; queue<syt> q; q.push(syt(a, b, c, 0)); int minv; while (!q.empty()) { if(movnum[q.front().a] > q.front().depth) movnum[q.front().a] = q.front().depth; if(movnum[q.front().b] > q.front().depth) movnum[q.front().b] = q.front().depth; if(movnum[q.front().c] > q.front().depth) movnum[q.front().c] = q.front().depth; if (m[make_tuple(q.front().a, q.front().b, q.front().c)] == 1) { q.pop(); continue; } m[make_tuple(q.front().a, q.front().b, q.front().c)] = 1; if (q.front().a > 0 && q.front().b < capacity[1]) { minv = q.front().a > (capacity[1] - q.front().b) ? (capacity[1] - q.front().b) : q.front().a; q.push(syt(q.front().a - minv, q.front().b + minv, q.front().c, q.front().depth+1)); } if (q.front().a > 0 && q.front().c < capacity[2]) { minv = q.front().a > (capacity[2] - q.front().c) ? (capacity[2] - q.front().c) : q.front().a; q.push(syt(q.front().a - minv, q.front().b, q.front().c + minv, q.front().depth+1)); } if (q.front().b > 0 && q.front().c < capacity[2]) { minv = q.front().b > (capacity[2] - q.front().c) ? (capacity[2] - q.front().c) : q.front().b; q.push(syt(q.front().a, q.front().b - minv, q.front().c + minv, q.front().depth+1)); } if (q.front().b > 0 && q.front().a < capacity[0]) { minv = q.front().b > (capacity[0] - q.front().a) ? (capacity[0] - q.front().a) : q.front().b; q.push(syt(q.front().a + minv, q.front().b - minv, q.front().c, q.front().depth+1)); } if (q.front().c > 0 && q.front().a < capacity[0]) { minv = q.front().c > (capacity[0] - q.front().a) ? (capacity[0] - q.front().a) : q.front().c; q.push(syt(q.front().a + minv, q.front().b, q.front().c - minv, q.front().depth+1)); } if (q.front().c > 0 && q.front().b < capacity[1]) { minv = q.front().c > (capacity[1] - q.front().b) ? (capacity[1] - q.front().b) : q.front().c; q.push(syt(q.front().a, q.front().b + minv, q.front().c - minv, q.front().depth+1)); } q.pop(); } for(int i=0;i<=capacity[2];i++) std::cout << (movnum[i] == std::numeric_limits<int>::max() ? -1 : movnum[i]) << ' '; 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 | #include <iostream> #include <queue> #include <map> #include <tuple> #include <limits> using namespace std; struct syt { int a, b, c, depth; syt(int _a, int _b, int _c, int _d) : a(_a), b(_b), c(_c), depth(_d) {} }; int main(int argc, char **argv) { int a, b, c; int capacity[3]; std::cin >> capacity[0] >> capacity[1] >> capacity[2] >> a >> b >> c; int movnum[capacity[2]+1]; for(int i=0;i<=capacity[2];i++) movnum[i] = std::numeric_limits<int>::max(); movnum[a] = 0; movnum[b] = 0; movnum[c] = 0; map<tuple<int, int, int>, int> m; queue<syt> q; q.push(syt(a, b, c, 0)); int minv; while (!q.empty()) { if(movnum[q.front().a] > q.front().depth) movnum[q.front().a] = q.front().depth; if(movnum[q.front().b] > q.front().depth) movnum[q.front().b] = q.front().depth; if(movnum[q.front().c] > q.front().depth) movnum[q.front().c] = q.front().depth; if (m[make_tuple(q.front().a, q.front().b, q.front().c)] == 1) { q.pop(); continue; } m[make_tuple(q.front().a, q.front().b, q.front().c)] = 1; if (q.front().a > 0 && q.front().b < capacity[1]) { minv = q.front().a > (capacity[1] - q.front().b) ? (capacity[1] - q.front().b) : q.front().a; q.push(syt(q.front().a - minv, q.front().b + minv, q.front().c, q.front().depth+1)); } if (q.front().a > 0 && q.front().c < capacity[2]) { minv = q.front().a > (capacity[2] - q.front().c) ? (capacity[2] - q.front().c) : q.front().a; q.push(syt(q.front().a - minv, q.front().b, q.front().c + minv, q.front().depth+1)); } if (q.front().b > 0 && q.front().c < capacity[2]) { minv = q.front().b > (capacity[2] - q.front().c) ? (capacity[2] - q.front().c) : q.front().b; q.push(syt(q.front().a, q.front().b - minv, q.front().c + minv, q.front().depth+1)); } if (q.front().b > 0 && q.front().a < capacity[0]) { minv = q.front().b > (capacity[0] - q.front().a) ? (capacity[0] - q.front().a) : q.front().b; q.push(syt(q.front().a + minv, q.front().b - minv, q.front().c, q.front().depth+1)); } if (q.front().c > 0 && q.front().a < capacity[0]) { minv = q.front().c > (capacity[0] - q.front().a) ? (capacity[0] - q.front().a) : q.front().c; q.push(syt(q.front().a + minv, q.front().b, q.front().c - minv, q.front().depth+1)); } if (q.front().c > 0 && q.front().b < capacity[1]) { minv = q.front().c > (capacity[1] - q.front().b) ? (capacity[1] - q.front().b) : q.front().c; q.push(syt(q.front().a, q.front().b + minv, q.front().c - minv, q.front().depth+1)); } q.pop(); } for(int i=0;i<=capacity[2];i++) std::cout << (movnum[i] == std::numeric_limits<int>::max() ? -1 : movnum[i]) << ' '; return 0; } |