#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define ST first #define ND second #define SIZE(a) ((int)a.size()) int main() { ios_base::sync_with_stdio(0); vector<int> total(3); for(int i=0; i < 3; i++) { cin >> total[i]; } vector<int> a(3); vector<int> res(total[2]+1, -1); int marked = 0; for(int i=0; i < 3; i++) { cin >> a[i]; if(res[a[i]] == -1) { marked++; res[a[i]] = 0; } } queue<pair<vector<int>, int>> q; q.push({a, 0}); set<vector<int>> visited; visited.insert(a); while(!q.empty() && marked <= total[2]) { auto x = q.front(); q.pop(); a = x.first; int t = x.second; for(int i=0; i < 3; i++) { for(int j=0; j < 3; j++) { if(i == j) { continue; } vector<int> b = a; int new_amount = min(total[j], b[j]+b[i]); b[i]-= new_amount-b[j]; b[j] = new_amount; if(!visited.count(b)) { q.push({b, t+1}); visited.insert(b); for(int y : b) { if(res[y] == -1) { res[y] = t+1; marked++; } } } } } } for(int r : res) { cout << r << " "; } cout << "\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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define ST first #define ND second #define SIZE(a) ((int)a.size()) int main() { ios_base::sync_with_stdio(0); vector<int> total(3); for(int i=0; i < 3; i++) { cin >> total[i]; } vector<int> a(3); vector<int> res(total[2]+1, -1); int marked = 0; for(int i=0; i < 3; i++) { cin >> a[i]; if(res[a[i]] == -1) { marked++; res[a[i]] = 0; } } queue<pair<vector<int>, int>> q; q.push({a, 0}); set<vector<int>> visited; visited.insert(a); while(!q.empty() && marked <= total[2]) { auto x = q.front(); q.pop(); a = x.first; int t = x.second; for(int i=0; i < 3; i++) { for(int j=0; j < 3; j++) { if(i == j) { continue; } vector<int> b = a; int new_amount = min(total[j], b[j]+b[i]); b[i]-= new_amount-b[j]; b[j] = new_amount; if(!visited.count(b)) { q.push({b, t+1}); visited.insert(b); for(int y : b) { if(res[y] == -1) { res[y] = t+1; marked++; } } } } } } for(int r : res) { cout << r << " "; } cout << "\n"; } |