#include <algorithm> #include <iostream> #include <tuple> #include <queue> #include <unordered_set> #include <unordered_map> using namespace std; typedef tuple<int, int, int> Bottles; typedef pair<int, Bottles> State; struct hash_tuple { size_t operator()(const Bottles& x) const { return get<0>(x) ^ get<1>(x) ^ get<2>(x); } }; int main() { std::ios::sync_with_stdio(false); int A, B, C; int a, b, c; cin >> A >> B >> C; cin >> a >> b >> c; int MAXES[] = {A, B, C}; queue<State> Q; unordered_set<Bottles, hash_tuple> visited; unordered_map<int, int> first_time; Bottles bottles(a, b, c); visited.insert(bottles); Q.push(State(0, bottles)); int time; int missing_values = C + 1; while(!Q.empty()) { State state = Q.front(); Q.pop(); time = get<0>(state); bottles = get<1>(state); // aktualizuj for(auto val: {get<0>(bottles), get<1>(bottles), get<2>(bottles)}) { if(first_time.find(val) == first_time.end()) { first_time[val] = time; --missing_values; } } if(missing_values == 0) { break; } // polewaj for(int from = 0; from < 3; ++from) { for(int to = 0; to < 3; ++to) { if(from == to) { continue; } int arr[] = {get<0>(bottles), get<1>(bottles), get<2>(bottles)}; arr[to] += arr[from]; if(arr[to] > MAXES[to]) { int over = arr[to] - MAXES[to]; arr[to] = MAXES[to]; arr[from] = over; } else { arr[from] = 0; } Bottles new_bottles(arr[0], arr[1], arr[2]); if(visited.find(new_bottles) == visited.end()) { visited.insert(new_bottles); Q.push(State(time + 1, new_bottles)); } } } } for(int i = 0; i <= C; ++i) { if(first_time.find(i) == first_time.end()) { cout << "-1 "; } else { cout << first_time[i] << " "; } } cout << endl; }
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 | #include <algorithm> #include <iostream> #include <tuple> #include <queue> #include <unordered_set> #include <unordered_map> using namespace std; typedef tuple<int, int, int> Bottles; typedef pair<int, Bottles> State; struct hash_tuple { size_t operator()(const Bottles& x) const { return get<0>(x) ^ get<1>(x) ^ get<2>(x); } }; int main() { std::ios::sync_with_stdio(false); int A, B, C; int a, b, c; cin >> A >> B >> C; cin >> a >> b >> c; int MAXES[] = {A, B, C}; queue<State> Q; unordered_set<Bottles, hash_tuple> visited; unordered_map<int, int> first_time; Bottles bottles(a, b, c); visited.insert(bottles); Q.push(State(0, bottles)); int time; int missing_values = C + 1; while(!Q.empty()) { State state = Q.front(); Q.pop(); time = get<0>(state); bottles = get<1>(state); // aktualizuj for(auto val: {get<0>(bottles), get<1>(bottles), get<2>(bottles)}) { if(first_time.find(val) == first_time.end()) { first_time[val] = time; --missing_values; } } if(missing_values == 0) { break; } // polewaj for(int from = 0; from < 3; ++from) { for(int to = 0; to < 3; ++to) { if(from == to) { continue; } int arr[] = {get<0>(bottles), get<1>(bottles), get<2>(bottles)}; arr[to] += arr[from]; if(arr[to] > MAXES[to]) { int over = arr[to] - MAXES[to]; arr[to] = MAXES[to]; arr[from] = over; } else { arr[from] = 0; } Bottles new_bottles(arr[0], arr[1], arr[2]); if(visited.find(new_bottles) == visited.end()) { visited.insert(new_bottles); Q.push(State(time + 1, new_bottles)); } } } } for(int i = 0; i <= C; ++i) { if(first_time.find(i) == first_time.end()) { cout << "-1 "; } else { cout << first_time[i] << " "; } } cout << endl; } |