#include <bits/stdc++.h> using namespace std; using state = vector<int>; const state SPECIAL = state({-1, -1, -1}); const int UNSET = -1; state capacity(3); state pour(state s, int from, int to) { int delta = min(s[from], capacity[to] - s[to]); s[from] -= delta; s[to] += delta; return s; } void change_state(set<state>& seen, queue<state>& q, state& current) { state changed; for(int from = 0; from < 3; from++) { for(int to = 0; to < 3; to++) { if (from != to && current[from]) { changed = pour(current, from, to); if(seen.find(changed) == seen.end()) { seen.insert(changed); q.push(changed); } } } } } int main(int argc, char* argv[]) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int moves = 0; set <state> seen; state current(3); cin >> capacity[0] >> capacity[1] >> capacity[2]; cin >> current[0] >> current[1] >> current[2]; vector<int> r(capacity[2] + 1, -1); queue<state> q; q.push(current); q.push(SPECIAL); while (!q.empty()) { current = q.front(); q.pop(); if (current == SPECIAL) { if (!q.empty()) q.push(SPECIAL); moves++; } else { for (auto v: current) r[v] = r[v] == UNSET ? moves : r[v]; change_state(seen, q, current); } } for (int i = 0; i < r.size(); i++) cout << r[i] << " \n"[i == r.size() - 1]; 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 | #include <bits/stdc++.h> using namespace std; using state = vector<int>; const state SPECIAL = state({-1, -1, -1}); const int UNSET = -1; state capacity(3); state pour(state s, int from, int to) { int delta = min(s[from], capacity[to] - s[to]); s[from] -= delta; s[to] += delta; return s; } void change_state(set<state>& seen, queue<state>& q, state& current) { state changed; for(int from = 0; from < 3; from++) { for(int to = 0; to < 3; to++) { if (from != to && current[from]) { changed = pour(current, from, to); if(seen.find(changed) == seen.end()) { seen.insert(changed); q.push(changed); } } } } } int main(int argc, char* argv[]) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int moves = 0; set <state> seen; state current(3); cin >> capacity[0] >> capacity[1] >> capacity[2]; cin >> current[0] >> current[1] >> current[2]; vector<int> r(capacity[2] + 1, -1); queue<state> q; q.push(current); q.push(SPECIAL); while (!q.empty()) { current = q.front(); q.pop(); if (current == SPECIAL) { if (!q.empty()) q.push(SPECIAL); moves++; } else { for (auto v: current) r[v] = r[v] == UNSET ? moves : r[v]; change_state(seen, q, current); } } for (int i = 0; i < r.size(); i++) cout << r[i] << " \n"[i == r.size() - 1]; return 0; } |