// Szymon Rusiecki (11.12.2021) #include <bits/stdc++.h> int up[3]; int in[3]; int output[100005]; class state { public: int value[3]; state(int *A); bool operator==(const state &other) const; bool operator<=(const state &other) const; bool operator>=(const state &other) const; bool operator<(const state &other) const; bool operator>(const state &other) const; state fill(int i, int j, int k); }; std::set<state> set; std::set<std::pair<int, state> > queue; void make_states(state x, int deep) { queue.erase(queue.begin()); state temp[6] = {x.fill(0, 1, 2), x.fill(0, 2, 1), x.fill(1, 0, 2), x.fill(1, 2, 0), x.fill(2, 1, 0), x.fill(2, 0, 1)}; for (int i = 0; i < 6; ++i) if (set.find(temp[i]) == set.end()) { queue.insert(std::make_pair(deep + 1, temp[i])); set.insert(temp[i]); for (int j = 0; j < 3; ++j) (output[temp[i].value[j]] == -1) ? output[temp[i].value[j]] = deep + 1 : output[temp[i].value[j]] = std::min(deep + 1, output[temp[i].value[j]]); } while(!queue.empty()) make_states(queue.begin()->second, queue.begin()->first); } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); for (int i = 0; i < 3; ++i) std::cin >> up[i]; for (int i = 0; i < 3; ++i) std::cin >> in[i]; for (int i = 0; i <= up[2]; ++i) output[i] = -1; for (int i = 0; i < 3; ++i) output[in[i]] = 0; state bottle(in); set.insert(bottle); queue.insert(std::make_pair(0, bottle)); make_states(bottle, 0); for (int i = 0; i <= up[2]; ++i) std::cout << output[i] << " "; return 0; } state::state(int *A) { for (int i = 0; i < 3; ++i) value[i] = A[i]; } bool state::operator==(const state &other) const{ for (int i = 0; i < 3; ++i) if (value[i] == other.value[i]) return false; return true; } bool state::operator<=(const state &other) const{ return !(*this > other); } bool state::operator>=(const state &other) const{ return !(*this < other); } bool state::operator<(const state &other) const{ if (value[0] < other.value[0]) return true; else if (value[0] > other.value[0]) return false; else { if (value[1] < other.value[1]) return true; else if (value[1] > other.value[1]) return false; else return value[2] < other.value[2]; } } bool state::operator>(const state &other) const{ if (value[0] > other.value[0]) return true; else if (value[0] < other.value[0]) return false; else { if (value[1] > other.value[1]) return true; else if (value[1] < other.value[1]) return false; else return value[2] > other.value[2]; } } state state::fill(int i, int j, int k) { int diff = std::min(value[i], up[j] - value[j]); int temp_tab[3] = {0, 0, 0}; temp_tab[i] = value[i] - diff; temp_tab[j] = value[j] + diff; temp_tab[k] = value[k]; state *temp_state = new state(temp_tab); return *temp_state; }
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 | // Szymon Rusiecki (11.12.2021) #include <bits/stdc++.h> int up[3]; int in[3]; int output[100005]; class state { public: int value[3]; state(int *A); bool operator==(const state &other) const; bool operator<=(const state &other) const; bool operator>=(const state &other) const; bool operator<(const state &other) const; bool operator>(const state &other) const; state fill(int i, int j, int k); }; std::set<state> set; std::set<std::pair<int, state> > queue; void make_states(state x, int deep) { queue.erase(queue.begin()); state temp[6] = {x.fill(0, 1, 2), x.fill(0, 2, 1), x.fill(1, 0, 2), x.fill(1, 2, 0), x.fill(2, 1, 0), x.fill(2, 0, 1)}; for (int i = 0; i < 6; ++i) if (set.find(temp[i]) == set.end()) { queue.insert(std::make_pair(deep + 1, temp[i])); set.insert(temp[i]); for (int j = 0; j < 3; ++j) (output[temp[i].value[j]] == -1) ? output[temp[i].value[j]] = deep + 1 : output[temp[i].value[j]] = std::min(deep + 1, output[temp[i].value[j]]); } while(!queue.empty()) make_states(queue.begin()->second, queue.begin()->first); } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); for (int i = 0; i < 3; ++i) std::cin >> up[i]; for (int i = 0; i < 3; ++i) std::cin >> in[i]; for (int i = 0; i <= up[2]; ++i) output[i] = -1; for (int i = 0; i < 3; ++i) output[in[i]] = 0; state bottle(in); set.insert(bottle); queue.insert(std::make_pair(0, bottle)); make_states(bottle, 0); for (int i = 0; i <= up[2]; ++i) std::cout << output[i] << " "; return 0; } state::state(int *A) { for (int i = 0; i < 3; ++i) value[i] = A[i]; } bool state::operator==(const state &other) const{ for (int i = 0; i < 3; ++i) if (value[i] == other.value[i]) return false; return true; } bool state::operator<=(const state &other) const{ return !(*this > other); } bool state::operator>=(const state &other) const{ return !(*this < other); } bool state::operator<(const state &other) const{ if (value[0] < other.value[0]) return true; else if (value[0] > other.value[0]) return false; else { if (value[1] < other.value[1]) return true; else if (value[1] > other.value[1]) return false; else return value[2] < other.value[2]; } } bool state::operator>(const state &other) const{ if (value[0] > other.value[0]) return true; else if (value[0] < other.value[0]) return false; else { if (value[1] > other.value[1]) return true; else if (value[1] < other.value[1]) return false; else return value[2] > other.value[2]; } } state state::fill(int i, int j, int k) { int diff = std::min(value[i], up[j] - value[j]); int temp_tab[3] = {0, 0, 0}; temp_tab[i] = value[i] - diff; temp_tab[j] = value[j] + diff; temp_tab[k] = value[k]; state *temp_state = new state(temp_tab); return *temp_state; } |