#include <bits/stdc++.h> using namespace std; // #define int long long // const int P = 1e9+7; int A,B,C; using T = tuple<int,int,int>; map<T, int> M; void bfs(int a, int b, int c) { T base_tuple = tuple<int,int,int>(a,b,c); M[base_tuple] = 0; queue<T> q; q.push(base_tuple); while(!q.empty()) { auto tup = q.front(); // std::cerr << get<0>(tup) << " " << get<1>(tup) << " " << get<2>(tup) << endl; int new_dist = M[tup] + 1; q.pop(); auto add = [&M, &q, &new_dist](T new_el){ if(M.count(new_el) == 0) { M[new_el] = new_dist; q.push(new_el); } }; { std::tie(a,b,c) = tup; // a b c // max(0, a+b-B), min(a+b, B) c add(tuple<int,int,int>(max(0, a+b-B), min(a+b, B), c)); // max(0, a+c-C), b, min(a+c, C) add(tuple<int,int,int>(max(0, a+c-C), b, min(a+c, C))); add(tuple<int,int,int>(a, max(0, b+c-C), min(b+c, C))); // min(a+b, A), max(0, a+b,A), c add(tuple<int,int,int>(min(a+b, A), max(0, a+b-A), c)); // min(a+c, A), b, max(0, a+c-A) add(tuple<int,int,int>(min(a+c, A), b, max(0, a+c-A))); // a, min(b+c, B), max(0, b+c-B) add(tuple<int,int,int>(a, min(b+c, B), max(0, b+c-B))); } } } void result() { vector<int> v(C+1, (1e9)); for(auto const x : M) { vector<int> vals = {get<0>(x.first), get<1>(x.first), get<2>(x.first)}; for(int y : vals) v[y] = min(x.second, v[y]); } for(int i=0; i<C+1; i++) { if(v[i] == 1e9) cout << -1 << " "; else cout << v[i] << " "; } cout << endl; } int main() { ios_base::sync_with_stdio(false); cin >> A >> B >> C; int a,b,c; cin >> a >> b >> c; bfs(a,b,c); result(); 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | #include <bits/stdc++.h> using namespace std; // #define int long long // const int P = 1e9+7; int A,B,C; using T = tuple<int,int,int>; map<T, int> M; void bfs(int a, int b, int c) { T base_tuple = tuple<int,int,int>(a,b,c); M[base_tuple] = 0; queue<T> q; q.push(base_tuple); while(!q.empty()) { auto tup = q.front(); // std::cerr << get<0>(tup) << " " << get<1>(tup) << " " << get<2>(tup) << endl; int new_dist = M[tup] + 1; q.pop(); auto add = [&M, &q, &new_dist](T new_el){ if(M.count(new_el) == 0) { M[new_el] = new_dist; q.push(new_el); } }; { std::tie(a,b,c) = tup; // a b c // max(0, a+b-B), min(a+b, B) c add(tuple<int,int,int>(max(0, a+b-B), min(a+b, B), c)); // max(0, a+c-C), b, min(a+c, C) add(tuple<int,int,int>(max(0, a+c-C), b, min(a+c, C))); add(tuple<int,int,int>(a, max(0, b+c-C), min(b+c, C))); // min(a+b, A), max(0, a+b,A), c add(tuple<int,int,int>(min(a+b, A), max(0, a+b-A), c)); // min(a+c, A), b, max(0, a+c-A) add(tuple<int,int,int>(min(a+c, A), b, max(0, a+c-A))); // a, min(b+c, B), max(0, b+c-B) add(tuple<int,int,int>(a, min(b+c, B), max(0, b+c-B))); } } } void result() { vector<int> v(C+1, (1e9)); for(auto const x : M) { vector<int> vals = {get<0>(x.first), get<1>(x.first), get<2>(x.first)}; for(int y : vals) v[y] = min(x.second, v[y]); } for(int i=0; i<C+1; i++) { if(v[i] == 1e9) cout << -1 << " "; else cout << v[i] << " "; } cout << endl; } int main() { ios_base::sync_with_stdio(false); cin >> A >> B >> C; int a,b,c; cin >> a >> b >> c; bfs(a,b,c); result(); return 0; } |