#include <bits/stdc++.h> using namespace std; #define ll long long #define db double #define fs first #define sc second #define fo(i, n) for(int i=0; i<n; i++) #define FO(i, k, n) for(int i=k;i<n;i++) #define chck(x) cout<<#x<<" "<<x<<"\n"; template<typename... T> void read(T&... args){ ((cin >> args), ...); } typedef array<int,4> mom; pair<int,int>g[4]; map<mom,bool>seen; map<mom,ll>dist; ll wyn[(int)1e5+7]; void bfs(){ ll l; mom atab; mom temptab; queue<mom>Q; Q.push({g[0].sc, g[1].sc, g[2].sc}); seen[{g[0].sc, g[1].sc, g[2].sc}] = 1; dist[{g[0].sc, g[1].sc, g[2].sc}] = 0; while(!Q.empty()){ atab = Q.front(); Q.pop(); l = dist[atab]; //fo(i,3) cout << atab[i] << " "; //cout << "\n"; fo(i, 3) if(atab[i] <= g[2].fs) wyn[atab[i]] = min(wyn[atab[i]], l); fo(i,3){ fo(j,3){ if(i == j) continue; fo(e,3) temptab[e] = atab[e]; temptab[i] = max(atab[i] - (g[j].fs - atab[j]),0); temptab[j] = atab[j] + (atab[i] - temptab[i]); if(!seen[temptab] or dist[temptab] > l+1){ dist[temptab] = l+1; seen[temptab] = 1; Q.push(temptab); } } } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); fill(wyn, wyn+int(1e5+4), LLONG_MAX); cin >> g[0].fs >> g[1].fs >> g[2].fs; cin >> g[0].sc >> g[1].sc >> g[2].sc; bfs(); fo(i, g[2].fs+1) cout << (wyn[i]!=LLONG_MAX ? wyn[i] : -1) << " "; }
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 | #include <bits/stdc++.h> using namespace std; #define ll long long #define db double #define fs first #define sc second #define fo(i, n) for(int i=0; i<n; i++) #define FO(i, k, n) for(int i=k;i<n;i++) #define chck(x) cout<<#x<<" "<<x<<"\n"; template<typename... T> void read(T&... args){ ((cin >> args), ...); } typedef array<int,4> mom; pair<int,int>g[4]; map<mom,bool>seen; map<mom,ll>dist; ll wyn[(int)1e5+7]; void bfs(){ ll l; mom atab; mom temptab; queue<mom>Q; Q.push({g[0].sc, g[1].sc, g[2].sc}); seen[{g[0].sc, g[1].sc, g[2].sc}] = 1; dist[{g[0].sc, g[1].sc, g[2].sc}] = 0; while(!Q.empty()){ atab = Q.front(); Q.pop(); l = dist[atab]; //fo(i,3) cout << atab[i] << " "; //cout << "\n"; fo(i, 3) if(atab[i] <= g[2].fs) wyn[atab[i]] = min(wyn[atab[i]], l); fo(i,3){ fo(j,3){ if(i == j) continue; fo(e,3) temptab[e] = atab[e]; temptab[i] = max(atab[i] - (g[j].fs - atab[j]),0); temptab[j] = atab[j] + (atab[i] - temptab[i]); if(!seen[temptab] or dist[temptab] > l+1){ dist[temptab] = l+1; seen[temptab] = 1; Q.push(temptab); } } } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); fill(wyn, wyn+int(1e5+4), LLONG_MAX); cin >> g[0].fs >> g[1].fs >> g[2].fs; cin >> g[0].sc >> g[1].sc >> g[2].sc; bfs(); fo(i, g[2].fs+1) cout << (wyn[i]!=LLONG_MAX ? wyn[i] : -1) << " "; } |