#include <stdio.h> #include <set> #include <queue> struct state { int bottle[3]; int& operator[](int i) { return bottle[i]; } }; bool operator<(const state& a, const state& b) { for (int i = 0; i < 3; i++) if (a.bottle[i] != b.bottle[i]) return a.bottle[i] < b.bottle[i]; return false; } int full[3] = {51, 67, 100}; std::set<state> visited; int min(int a, int b) { if (a < b) return a; return b; } state move_states(state s, int f, int t) { state answer = s; int amount = min(s[f], full[t] - s[t]); answer[f] -= amount; answer[t] += amount; return answer; } const int mf[] = {1, 1, 2, 2, 0, 0}; const int mt[] = {2, 0, 1, 0, 1, 2}; const int max_C = 1e5; int answer[max_C+1]; void count_states(std::pair<state, int> s) { std::queue<std::pair<state, int>> Q; Q.push(s); while (!Q.empty()) { state s = Q.front().first; int moves = Q.front().second; Q.pop(); for (int j = 0; j < 3; j++) if (answer[s[j]] == -1) answer[s[j]] = moves; if (visited.count(s) == 0) { visited.insert(s); for (int i = 0; i < 6; i++) { state news = move_states(s, mf[i], mt[i]); Q.push(std::make_pair(news, moves+1)); } } } } int main() { state input; for (int i = 0; i < 3; i++) scanf("%d", &full[i]); for (int i = 0; i < 3; i++) scanf("%d", &input[i]); for (int i = 0; i <= full[2]; i++) answer[i] = -1; count_states(std::make_pair(input, 0)); for (int i = 0; i <= full[2]; i++) printf("%d ", answer[i]); 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 | #include <stdio.h> #include <set> #include <queue> struct state { int bottle[3]; int& operator[](int i) { return bottle[i]; } }; bool operator<(const state& a, const state& b) { for (int i = 0; i < 3; i++) if (a.bottle[i] != b.bottle[i]) return a.bottle[i] < b.bottle[i]; return false; } int full[3] = {51, 67, 100}; std::set<state> visited; int min(int a, int b) { if (a < b) return a; return b; } state move_states(state s, int f, int t) { state answer = s; int amount = min(s[f], full[t] - s[t]); answer[f] -= amount; answer[t] += amount; return answer; } const int mf[] = {1, 1, 2, 2, 0, 0}; const int mt[] = {2, 0, 1, 0, 1, 2}; const int max_C = 1e5; int answer[max_C+1]; void count_states(std::pair<state, int> s) { std::queue<std::pair<state, int>> Q; Q.push(s); while (!Q.empty()) { state s = Q.front().first; int moves = Q.front().second; Q.pop(); for (int j = 0; j < 3; j++) if (answer[s[j]] == -1) answer[s[j]] = moves; if (visited.count(s) == 0) { visited.insert(s); for (int i = 0; i < 6; i++) { state news = move_states(s, mf[i], mt[i]); Q.push(std::make_pair(news, moves+1)); } } } } int main() { state input; for (int i = 0; i < 3; i++) scanf("%d", &full[i]); for (int i = 0; i < 3; i++) scanf("%d", &input[i]); for (int i = 0; i <= full[2]; i++) answer[i] = -1; count_states(std::make_pair(input, 0)); for (int i = 0; i <= full[2]; i++) printf("%d ", answer[i]); printf("\n"); return 0; } |