#include <iostream> #include <vector> #include <algorithm> #include <queue> #include <map> const int NUMBER_OF_BOTTLES = 3; const int MAX_COUNTER = 7; const int INF = 1000000007; const long long MAX_CAPACITY = 100001; int A, B, C; /*int solve(int a, int b, int c, int counter, int value) { int result = INF; if (a == value || b == value || c == value) return 0; if (counter == 0) return INF; result = std::min(result, 1 + solve(std::max(a + b - B, 0), std::min(a + b, B), c, counter - 1, value)); result = std::min(result, 1 + solve(std::max(a + c - C, 0), b, std::min(a + c, C), counter - 1, value)); result = std::min(result, 1 + solve(std::min(a + b, A), std::max(a + b - A, 0), c, counter - 1, value)); result = std::min(result, 1 + solve(a, std::max(b + c - C, 0), std::min(b + c, C), counter - 1, value)); result = std::min(result, 1 + solve(std::min(a + c, A), b, std::max(a + c - A, 0), counter - 1, value)); result = std::min(result, 1 + solve(a, std::min(b + c, B), std::max(b + c - B, 0), counter - 1, value)); return result; }*/ struct decrypted { long long a; long long b; long long c; }; inline long long crypt(int a, int b, int c) { return a + b * MAX_CAPACITY + c * MAX_CAPACITY * MAX_CAPACITY; }; inline decrypted decrypt(long long crypted) { return {crypted % MAX_CAPACITY, (crypted / MAX_CAPACITY) % MAX_CAPACITY, (crypted / (MAX_CAPACITY * MAX_CAPACITY) % MAX_CAPACITY)}; } //std::vector<std::vector<std::vector<int>>> distance; std::map<long long, int> distance; std::vector<int> results; std::pair<long long, long long> przelej(long long a, long long b, long long B) { long long result_b = a + b; if (result_b > B) result_b = B; long long result_a = a + b - result_b; return {result_a, result_b}; } void BFS(int a, int b, int c) { long long crypted = crypt(a, b, c); distance.insert({crypted, 0}); std::queue<long long> bfs_queue; bfs_queue.push(crypted); while (!bfs_queue.empty()) { long long crypted = bfs_queue.front(); bfs_queue.pop(); decrypted dupa = decrypt(crypted); long long a = dupa.a; long long b = dupa.b; long long c = dupa.c; int current_distance = distance[crypted]; results[a] = std::min(results[a], current_distance); results[b] = std::min(results[b], current_distance); results[c] = std::min(results[c], current_distance); auto przelej_result = przelej(a, b, B); crypted = crypt(przelej_result.first, przelej_result.second, c); auto d = distance.find(crypted); if (d == distance.end()) { distance.insert({crypted, current_distance + 1}); bfs_queue.push(crypt(przelej_result.first, przelej_result.second, c)); } przelej_result = przelej(a, c, C); crypted = crypt(przelej_result.first, b, przelej_result.second); d = distance.find(crypted); if (d == distance.end()) { distance.insert({crypted, current_distance + 1}); bfs_queue.push(crypt(przelej_result.first, b, przelej_result.second)); } przelej_result = przelej(b, a, A); crypted = crypt(przelej_result.second, przelej_result.first, c); d = distance.find(crypted); if (d == distance.end()) { distance.insert({crypted, current_distance + 1}); bfs_queue.push(crypt(przelej_result.second, przelej_result.first, c)); } przelej_result = przelej(b, c, C); crypted = crypt(a, przelej_result.first, przelej_result.second); d = distance.find(crypted); if (d == distance.end()) { distance.insert({crypted, current_distance + 1}); bfs_queue.push(crypt(a, przelej_result.first, przelej_result.second)); } przelej_result = przelej(c, a, A); crypted = crypt(przelej_result.second, b, przelej_result.first); d = distance.find(crypted); if (d == distance.end()) { distance.insert({crypted, current_distance + 1}); bfs_queue.push(crypt(przelej_result.second, b, przelej_result.first)); } przelej_result = przelej(c, b, B); crypted = crypt(a, przelej_result.second, przelej_result.first); d = distance.find(crypted); if (d == distance.end()) { distance.insert({crypted, current_distance + 1}); bfs_queue.push(crypt(a, przelej_result.second, przelej_result.first)); } } } int main() { scanf("%d %d %d", &A, &B, &C); int a, b, c; scanf("%d %d %d", &a, &b, &c); results.assign(C + 1, INF); BFS(a, b, c); for (int i = 0; i <= C; i++) { if (results[i] >= INF) { printf("-1 "); } else { printf("%d ", results[i]); } } printf("\n"); }
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 <vector> #include <algorithm> #include <queue> #include <map> const int NUMBER_OF_BOTTLES = 3; const int MAX_COUNTER = 7; const int INF = 1000000007; const long long MAX_CAPACITY = 100001; int A, B, C; /*int solve(int a, int b, int c, int counter, int value) { int result = INF; if (a == value || b == value || c == value) return 0; if (counter == 0) return INF; result = std::min(result, 1 + solve(std::max(a + b - B, 0), std::min(a + b, B), c, counter - 1, value)); result = std::min(result, 1 + solve(std::max(a + c - C, 0), b, std::min(a + c, C), counter - 1, value)); result = std::min(result, 1 + solve(std::min(a + b, A), std::max(a + b - A, 0), c, counter - 1, value)); result = std::min(result, 1 + solve(a, std::max(b + c - C, 0), std::min(b + c, C), counter - 1, value)); result = std::min(result, 1 + solve(std::min(a + c, A), b, std::max(a + c - A, 0), counter - 1, value)); result = std::min(result, 1 + solve(a, std::min(b + c, B), std::max(b + c - B, 0), counter - 1, value)); return result; }*/ struct decrypted { long long a; long long b; long long c; }; inline long long crypt(int a, int b, int c) { return a + b * MAX_CAPACITY + c * MAX_CAPACITY * MAX_CAPACITY; }; inline decrypted decrypt(long long crypted) { return {crypted % MAX_CAPACITY, (crypted / MAX_CAPACITY) % MAX_CAPACITY, (crypted / (MAX_CAPACITY * MAX_CAPACITY) % MAX_CAPACITY)}; } //std::vector<std::vector<std::vector<int>>> distance; std::map<long long, int> distance; std::vector<int> results; std::pair<long long, long long> przelej(long long a, long long b, long long B) { long long result_b = a + b; if (result_b > B) result_b = B; long long result_a = a + b - result_b; return {result_a, result_b}; } void BFS(int a, int b, int c) { long long crypted = crypt(a, b, c); distance.insert({crypted, 0}); std::queue<long long> bfs_queue; bfs_queue.push(crypted); while (!bfs_queue.empty()) { long long crypted = bfs_queue.front(); bfs_queue.pop(); decrypted dupa = decrypt(crypted); long long a = dupa.a; long long b = dupa.b; long long c = dupa.c; int current_distance = distance[crypted]; results[a] = std::min(results[a], current_distance); results[b] = std::min(results[b], current_distance); results[c] = std::min(results[c], current_distance); auto przelej_result = przelej(a, b, B); crypted = crypt(przelej_result.first, przelej_result.second, c); auto d = distance.find(crypted); if (d == distance.end()) { distance.insert({crypted, current_distance + 1}); bfs_queue.push(crypt(przelej_result.first, przelej_result.second, c)); } przelej_result = przelej(a, c, C); crypted = crypt(przelej_result.first, b, przelej_result.second); d = distance.find(crypted); if (d == distance.end()) { distance.insert({crypted, current_distance + 1}); bfs_queue.push(crypt(przelej_result.first, b, przelej_result.second)); } przelej_result = przelej(b, a, A); crypted = crypt(przelej_result.second, przelej_result.first, c); d = distance.find(crypted); if (d == distance.end()) { distance.insert({crypted, current_distance + 1}); bfs_queue.push(crypt(przelej_result.second, przelej_result.first, c)); } przelej_result = przelej(b, c, C); crypted = crypt(a, przelej_result.first, przelej_result.second); d = distance.find(crypted); if (d == distance.end()) { distance.insert({crypted, current_distance + 1}); bfs_queue.push(crypt(a, przelej_result.first, przelej_result.second)); } przelej_result = przelej(c, a, A); crypted = crypt(przelej_result.second, b, przelej_result.first); d = distance.find(crypted); if (d == distance.end()) { distance.insert({crypted, current_distance + 1}); bfs_queue.push(crypt(przelej_result.second, b, przelej_result.first)); } przelej_result = przelej(c, b, B); crypted = crypt(a, przelej_result.second, przelej_result.first); d = distance.find(crypted); if (d == distance.end()) { distance.insert({crypted, current_distance + 1}); bfs_queue.push(crypt(a, przelej_result.second, przelej_result.first)); } } } int main() { scanf("%d %d %d", &A, &B, &C); int a, b, c; scanf("%d %d %d", &a, &b, &c); results.assign(C + 1, INF); BFS(a, b, c); for (int i = 0; i <= C; i++) { if (results[i] >= INF) { printf("-1 "); } else { printf("%d ", results[i]); } } printf("\n"); } |