#include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> #define ST first #define ND second #define ll long long #define ld long double #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " using namespace std; using namespace __gnu_pbds; // replace ll with pair if multiset needed typedef tree<ll, null_type, greater<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; ordered_set secik; const ll INF = 1e9 + 9; const ll MOD = 1e9 + 7; const long long LINF = (ll)1e18 + 3; // secik.insert({x, t++}); // secik.erase(secik.lower_bound({x,-1})); // *secik.find_by_order(x)).first << "\n"; // secik.order_of_key(x) //random_device device; //mt19937 gener(device()); //uniform_int_distribution<ll > gen(0,n-1); //gen(gener); // generate random number const int SIZE = 1e5 + 5; int tab[SIZE]; map<pair<int,pair<int,int>>,int> mapa; int A,B,C; pair<pair<int,int>,pair<int,int> > parowanie(int a, int b, int c, int d){ return make_pair(make_pair(a,b),make_pair(c,d)); } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> A >> B >> C; for(int i = 0; i <= C; i++) tab[i] = 1e9; int x,y,z; cin >> x >> y >> z; queue<pair<pair<int,int>,pair<int,int> > > q; q.push({{x,y},{z,1}}); while(!q.empty()){ auto it = q.front(); q.pop(); x = it.ST.ST, y = it.ST.ND, z = it.ND.ST; int dep = it.ND.ND; int tmp = mapa[{x,{y,z}}]; if(tmp == 0 || dep < tmp){ tab[x] = min(tab[x],dep), tab[y] = min(tab[y],dep), tab[z] = min(tab[z],dep); mapa[{x,{y,z}}] = dep; //cout << dep << endl; // x do y if(y+x > B) q.push(parowanie(x + y - B, B,z,dep+1)); else q.push(parowanie(0, x + y,z,dep+1)); // x do z if(x+z > C) q.push(parowanie(x + z - C,y,C,dep+1)); else q.push(parowanie(0, y,x + z,dep+1)); // y do x if(y+x > A) q.push(parowanie(A, x+y-A,z,dep+1)); else q.push(parowanie(x+y,0,z,dep+1)); // y do z if(y+z > C) q.push(parowanie(x, y+z-C,C,dep+1)); else q.push(parowanie(x,0,y+z,dep+1)); // z do x if(z+x > A) q.push(parowanie(A, y,z+x-A,dep+1)); else q.push(parowanie(z+x, y,0,dep+1)); // z do y if(z+y > B) q.push(parowanie(x, B,z+y-B,dep+1)); else q.push(parowanie(x, z+y,0,dep+1)); } else { continue; } } //biegnij(x,y,z); for(int i = 0; i <= C; i++){ if(tab[i] != 1e9) cout << tab[i] - 1 << " "; else cout << -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 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 | #include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> #define ST first #define ND second #define ll long long #define ld long double #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " using namespace std; using namespace __gnu_pbds; // replace ll with pair if multiset needed typedef tree<ll, null_type, greater<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; ordered_set secik; const ll INF = 1e9 + 9; const ll MOD = 1e9 + 7; const long long LINF = (ll)1e18 + 3; // secik.insert({x, t++}); // secik.erase(secik.lower_bound({x,-1})); // *secik.find_by_order(x)).first << "\n"; // secik.order_of_key(x) //random_device device; //mt19937 gener(device()); //uniform_int_distribution<ll > gen(0,n-1); //gen(gener); // generate random number const int SIZE = 1e5 + 5; int tab[SIZE]; map<pair<int,pair<int,int>>,int> mapa; int A,B,C; pair<pair<int,int>,pair<int,int> > parowanie(int a, int b, int c, int d){ return make_pair(make_pair(a,b),make_pair(c,d)); } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> A >> B >> C; for(int i = 0; i <= C; i++) tab[i] = 1e9; int x,y,z; cin >> x >> y >> z; queue<pair<pair<int,int>,pair<int,int> > > q; q.push({{x,y},{z,1}}); while(!q.empty()){ auto it = q.front(); q.pop(); x = it.ST.ST, y = it.ST.ND, z = it.ND.ST; int dep = it.ND.ND; int tmp = mapa[{x,{y,z}}]; if(tmp == 0 || dep < tmp){ tab[x] = min(tab[x],dep), tab[y] = min(tab[y],dep), tab[z] = min(tab[z],dep); mapa[{x,{y,z}}] = dep; //cout << dep << endl; // x do y if(y+x > B) q.push(parowanie(x + y - B, B,z,dep+1)); else q.push(parowanie(0, x + y,z,dep+1)); // x do z if(x+z > C) q.push(parowanie(x + z - C,y,C,dep+1)); else q.push(parowanie(0, y,x + z,dep+1)); // y do x if(y+x > A) q.push(parowanie(A, x+y-A,z,dep+1)); else q.push(parowanie(x+y,0,z,dep+1)); // y do z if(y+z > C) q.push(parowanie(x, y+z-C,C,dep+1)); else q.push(parowanie(x,0,y+z,dep+1)); // z do x if(z+x > A) q.push(parowanie(A, y,z+x-A,dep+1)); else q.push(parowanie(z+x, y,0,dep+1)); // z do y if(z+y > B) q.push(parowanie(x, B,z+y-B,dep+1)); else q.push(parowanie(x, z+y,0,dep+1)); } else { continue; } } //biegnij(x,y,z); for(int i = 0; i <= C; i++){ if(tab[i] != 1e9) cout << tab[i] - 1 << " "; else cout << -1 << " "; } return 0; } |