#include <iostream> #include <cstdio> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <algorithm> #include <cstring> #include <ctime> #include <cassert> using namespace std; #define assert_range(x,a,b) assert((a) <= (x) and (x) <= (b)) const int R = 3; int cap[R]; int total; using State = vector<int>; using Values = vector<int>; Values decode(State s) { int i = s[0]; int vi = s[1] ? cap[i] : 0; int j = s[2]; int vj = s[3]; int k = 0; while (k == i || k == j) ++k; int vk = total - vi - vj; assert(0 <= vk && vk <= cap[k]); Values res(R); res[i] = vi; res[j] = vj; res[k] = vk; return res; } State encode(Values vals) { int i = -1; int ifull; int j = -1; int vj; for (int idx = 0; idx < R && (i == -1 || j == -1); ++idx) { int v = vals[idx]; if (i == -1 && (v == 0 || v == cap[idx])) { i = idx; ifull = v == 0 ? 0 : 1; } else if (j == -1) { j = idx; vj = v; } } assert(i != -1 && j != -1); assert(vj >= 0); return {i, ifull, j, vj}; } vector<Values> get_moves(Values vals) { vector<Values> moves; for (int i = 0; i < R; ++i) { for (int j = 0; j < R; ++j) { if (i == j) continue; // pour from i to j int vi = vals[i]; int ci = cap[i]; int vj = vals[j]; int cj = cap[j]; int rem = vi; int avi = cj-vj; Values new_vals = vals; if (rem <= avi) { // empty i new_vals[j] += rem; new_vals[i] = 0; } else { // pour exactly avi new_vals[j] += avi; new_vals[i] -= avi; } moves.push_back(new_vals); } } return moves; } void show(vector<int> v) { for (auto x : v) { cout << x << " "; } cout << endl; } int main() { scanf("%d %d %d", &cap[0], &cap[1], &cap[2]); int a, b, c; scanf("%d %d %d", &a, &b, &c); total = a+b+c; map<State, int> dst; deque<pair<Values, int>> q = {{{a, b, c}, 0}}; while (!q.empty()) { auto vals = q[0].first; int d = q[0].second; q.pop_front(); for (auto new_vals : get_moves(vals)) { auto ns = encode(new_vals); if (dst.find(ns) == dst.end()) { dst[ns] = d+1; q.push_back({new_vals, d+1}); } } } vector<int> res(cap[2]+1, -1); res[a] = 0; res[b] = 0; res[c] = 0; for (auto it = dst.begin(); it != dst.end(); ++it) { auto vals = decode(it->first); auto d = it->second; for (auto v : vals) { if (res[v] == -1 || d < res[v]) { res[v] = d; } } } for (auto d : res) { printf("%d ", d); } printf("\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 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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | #include <iostream> #include <cstdio> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <algorithm> #include <cstring> #include <ctime> #include <cassert> using namespace std; #define assert_range(x,a,b) assert((a) <= (x) and (x) <= (b)) const int R = 3; int cap[R]; int total; using State = vector<int>; using Values = vector<int>; Values decode(State s) { int i = s[0]; int vi = s[1] ? cap[i] : 0; int j = s[2]; int vj = s[3]; int k = 0; while (k == i || k == j) ++k; int vk = total - vi - vj; assert(0 <= vk && vk <= cap[k]); Values res(R); res[i] = vi; res[j] = vj; res[k] = vk; return res; } State encode(Values vals) { int i = -1; int ifull; int j = -1; int vj; for (int idx = 0; idx < R && (i == -1 || j == -1); ++idx) { int v = vals[idx]; if (i == -1 && (v == 0 || v == cap[idx])) { i = idx; ifull = v == 0 ? 0 : 1; } else if (j == -1) { j = idx; vj = v; } } assert(i != -1 && j != -1); assert(vj >= 0); return {i, ifull, j, vj}; } vector<Values> get_moves(Values vals) { vector<Values> moves; for (int i = 0; i < R; ++i) { for (int j = 0; j < R; ++j) { if (i == j) continue; // pour from i to j int vi = vals[i]; int ci = cap[i]; int vj = vals[j]; int cj = cap[j]; int rem = vi; int avi = cj-vj; Values new_vals = vals; if (rem <= avi) { // empty i new_vals[j] += rem; new_vals[i] = 0; } else { // pour exactly avi new_vals[j] += avi; new_vals[i] -= avi; } moves.push_back(new_vals); } } return moves; } void show(vector<int> v) { for (auto x : v) { cout << x << " "; } cout << endl; } int main() { scanf("%d %d %d", &cap[0], &cap[1], &cap[2]); int a, b, c; scanf("%d %d %d", &a, &b, &c); total = a+b+c; map<State, int> dst; deque<pair<Values, int>> q = {{{a, b, c}, 0}}; while (!q.empty()) { auto vals = q[0].first; int d = q[0].second; q.pop_front(); for (auto new_vals : get_moves(vals)) { auto ns = encode(new_vals); if (dst.find(ns) == dst.end()) { dst[ns] = d+1; q.push_back({new_vals, d+1}); } } } vector<int> res(cap[2]+1, -1); res[a] = 0; res[b] = 0; res[c] = 0; for (auto it = dst.begin(); it != dst.end(); ++it) { auto vals = decode(it->first); auto d = it->second; for (auto v : vals) { if (res[v] == -1 || d < res[v]) { res[v] = d; } } } for (auto d : res) { printf("%d ", d); } printf("\n"); return 0; } |