#include <cstdio> #include <map> #include <vector> typedef struct pos { int bot[3]; } pos; bool operator<(const pos &X, const pos &Y) { for (int i = 0; i < 3; ++i) if (X.bot[i] != Y.bot[i]) return X.bot[i] < Y.bot[i]; return false; } std::map<pos, int> poss; std::vector<pos> bfs; int res[100100]; int lim[3]; void pour(const pos &Y, int from, int to) { pos X = Y; if (X.bot[from] + X.bot[to] <= lim[to]) { X.bot[to] += X.bot[from]; X.bot[from] = 0; } else { X.bot[from] = X.bot[from] + X.bot[to] - lim[to]; X.bot[to] = lim[to]; } if (poss.find(X) != poss.end()) return; poss[X] = poss[Y] + 1; bfs.push_back(X); } void process(pos X) { for (int i = 0; i < 3; ++i) { if (res[X.bot[i]] == -1) res[X.bot[i]] = poss[X]; } for (int from = 0; from < 3; ++from) for (int to = 0; to < 3; ++to) if (from != to) { pour(X, from, to); } } int main() { pos start; scanf("%d %d %d %d %d %d", &lim[0], &lim[1], &lim[2], &start.bot[0], &start.bot[1], &start.bot[2]); for (int i = 0; i <= lim[2]; i++) res[i] = -1; poss[start] = 0; bfs.push_back(start); int index = 0; while (index < (int) bfs.size()) { process(bfs[index++]); } for (int i = 0; i <= lim[2]; ++i) printf("%d ", res[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 | #include <cstdio> #include <map> #include <vector> typedef struct pos { int bot[3]; } pos; bool operator<(const pos &X, const pos &Y) { for (int i = 0; i < 3; ++i) if (X.bot[i] != Y.bot[i]) return X.bot[i] < Y.bot[i]; return false; } std::map<pos, int> poss; std::vector<pos> bfs; int res[100100]; int lim[3]; void pour(const pos &Y, int from, int to) { pos X = Y; if (X.bot[from] + X.bot[to] <= lim[to]) { X.bot[to] += X.bot[from]; X.bot[from] = 0; } else { X.bot[from] = X.bot[from] + X.bot[to] - lim[to]; X.bot[to] = lim[to]; } if (poss.find(X) != poss.end()) return; poss[X] = poss[Y] + 1; bfs.push_back(X); } void process(pos X) { for (int i = 0; i < 3; ++i) { if (res[X.bot[i]] == -1) res[X.bot[i]] = poss[X]; } for (int from = 0; from < 3; ++from) for (int to = 0; to < 3; ++to) if (from != to) { pour(X, from, to); } } int main() { pos start; scanf("%d %d %d %d %d %d", &lim[0], &lim[1], &lim[2], &start.bot[0], &start.bot[1], &start.bot[2]); for (int i = 0; i <= lim[2]; i++) res[i] = -1; poss[start] = 0; bfs.push_back(start); int index = 0; while (index < (int) bfs.size()) { process(bfs[index++]); } for (int i = 0; i <= lim[2]; ++i) printf("%d ", res[i]); printf("\n"); return 0; } |