#include<bits/stdc++.h> using namespace std; const int inf=1000000007; int main(){ long long A,B,C,a,b,c,x,y,z,tmp,d,i; set <long long> S; queue <pair<long long,int>> Q; scanf("%lld%lld%lld",&A,&B,&C); scanf("%lld%lld%lld",&a,&b,&c); int ans[C+1]; for(i=0;i<=C;i++) ans[i]=inf; Q.push({a+(A+1)*b+(A+1)*(B+1)*c,0}); while(!Q.empty()){ tmp=Q.front().first; a=tmp%(A+1); tmp/=(A+1); b=tmp%(B+1); tmp/=(B+1); c=tmp; d=Q.front().second; Q.pop(); if(S.find(a+(A+1)*b+(A+1)*(B+1)*c)!=S.end()) continue; S.insert(a+(A+1)*b+(A+1)*(B+1)*c); ans[a]=min(ans[a],(int)d); ans[b]=min(ans[b],(int)d); ans[c]=min(ans[c],(int)d); d++; x=min(A,a+b); y=max(0ll,a+b-A); z=c; if(S.find(x+(A+1)*y+(A+1)*(B+1)*z)==S.end()) Q.push({x+(A+1)*y+(A+1)*(B+1)*z,d}); x=min(A,a+c); y=b; z=max(0ll,a+c-A); if(S.find(x+(A+1)*y+(A+1)*(B+1)*z)==S.end()) Q.push({x+(A+1)*y+(A+1)*(B+1)*z,d}); x=max(0ll,a+b-B); y=min(B,a+b); z=c; if(S.find(x+(A+1)*y+(A+1)*(B+1)*z)==S.end()) Q.push({x+(A+1)*y+(A+1)*(B+1)*z,d}); x=a; y=min(B,b+c); z=max(0ll,b+c-B); if(S.find(x+(A+1)*y+(A+1)*(B+1)*z)==S.end()) Q.push({x+(A+1)*y+(A+1)*(B+1)*z,d}); x=max(0ll,a+c-C); y=b; z=min(C,a+c); if(S.find(x+(A+1)*y+(A+1)*(B+1)*z)==S.end()) Q.push({x+(A+1)*y+(A+1)*(B+1)*z,d}); x=a; y=max(0ll,b+c-C); z=min(C,b+c); if(S.find(x+(A+1)*y+(A+1)*(B+1)*z)==S.end()) Q.push({x+(A+1)*y+(A+1)*(B+1)*z,d}); } for(i=0;i<=C;i++) if(ans[i]!=inf) printf("%d ",ans[i]); else printf("-1 "); printf("\n"); 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 | #include<bits/stdc++.h> using namespace std; const int inf=1000000007; int main(){ long long A,B,C,a,b,c,x,y,z,tmp,d,i; set <long long> S; queue <pair<long long,int>> Q; scanf("%lld%lld%lld",&A,&B,&C); scanf("%lld%lld%lld",&a,&b,&c); int ans[C+1]; for(i=0;i<=C;i++) ans[i]=inf; Q.push({a+(A+1)*b+(A+1)*(B+1)*c,0}); while(!Q.empty()){ tmp=Q.front().first; a=tmp%(A+1); tmp/=(A+1); b=tmp%(B+1); tmp/=(B+1); c=tmp; d=Q.front().second; Q.pop(); if(S.find(a+(A+1)*b+(A+1)*(B+1)*c)!=S.end()) continue; S.insert(a+(A+1)*b+(A+1)*(B+1)*c); ans[a]=min(ans[a],(int)d); ans[b]=min(ans[b],(int)d); ans[c]=min(ans[c],(int)d); d++; x=min(A,a+b); y=max(0ll,a+b-A); z=c; if(S.find(x+(A+1)*y+(A+1)*(B+1)*z)==S.end()) Q.push({x+(A+1)*y+(A+1)*(B+1)*z,d}); x=min(A,a+c); y=b; z=max(0ll,a+c-A); if(S.find(x+(A+1)*y+(A+1)*(B+1)*z)==S.end()) Q.push({x+(A+1)*y+(A+1)*(B+1)*z,d}); x=max(0ll,a+b-B); y=min(B,a+b); z=c; if(S.find(x+(A+1)*y+(A+1)*(B+1)*z)==S.end()) Q.push({x+(A+1)*y+(A+1)*(B+1)*z,d}); x=a; y=min(B,b+c); z=max(0ll,b+c-B); if(S.find(x+(A+1)*y+(A+1)*(B+1)*z)==S.end()) Q.push({x+(A+1)*y+(A+1)*(B+1)*z,d}); x=max(0ll,a+c-C); y=b; z=min(C,a+c); if(S.find(x+(A+1)*y+(A+1)*(B+1)*z)==S.end()) Q.push({x+(A+1)*y+(A+1)*(B+1)*z,d}); x=a; y=max(0ll,b+c-C); z=min(C,b+c); if(S.find(x+(A+1)*y+(A+1)*(B+1)*z)==S.end()) Q.push({x+(A+1)*y+(A+1)*(B+1)*z,d}); } for(i=0;i<=C;i++) if(ans[i]!=inf) printf("%d ",ans[i]); else printf("-1 "); printf("\n"); return 0; } |