#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <set>
#include <queue>
using namespace std;
const long long M = 1000000L;
set<long long> S;
long long Q[M*2];
int K[M*2];
int res[100010];
int cap[3];
int act[3];
int nact[3];
int r;
int main()
{
for (int i=0;i<3;i++) cin >> cap[i];
for (int i=0;i<3;i++) cin >> act[i];
for (int i=0;i<=cap[2]+1;i++) res[i]=M*2;
long long nx = M*M*act[2] + M*act[1] + act[0];
S.insert(nx);
Q[0]=nx;
K[0]=0;
int h = 1, t = 0,k;
long long ac;
long long na,nb,nc;
while (t < h)
{
ac = Q[t];
k = K[t];
t++;
act[0] = ac % M;
ac /= M;
act[1] = ac % M;
ac /= M;
act[2] = ac;
for (int i=0;i<3;i++) res[act[i]]=min(res[act[i]],k);
for (int from=0;from<3;from++)
{
for(int to=0;to<3;to++)
{
if (from==to) continue;
r=min(act[from], cap[to]-act[to]);
if (r == 0) continue;
for (int w=0;w<3;w++) nact[w]=act[w];
nact[from]-=r;
nact[to]+=r;
nx = M*M*nact[2] + M*nact[1] + nact[0];
if (S.find(nx) == S.end())
{
S.insert(nx);
Q[h]=nx;
K[h]=k+1;
h++;
}
}
}
k++;
}
for (int i=0;i<=cap[2];i++)
{
printf("%d ", (res[i] == M*2 ? -1 : res[i]));
}
}
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 | #include <iostream> #include <vector> #include <algorithm> #include <cstring> #include <set> #include <queue> using namespace std; const long long M = 1000000L; set<long long> S; long long Q[M*2]; int K[M*2]; int res[100010]; int cap[3]; int act[3]; int nact[3]; int r; int main() { for (int i=0;i<3;i++) cin >> cap[i]; for (int i=0;i<3;i++) cin >> act[i]; for (int i=0;i<=cap[2]+1;i++) res[i]=M*2; long long nx = M*M*act[2] + M*act[1] + act[0]; S.insert(nx); Q[0]=nx; K[0]=0; int h = 1, t = 0,k; long long ac; long long na,nb,nc; while (t < h) { ac = Q[t]; k = K[t]; t++; act[0] = ac % M; ac /= M; act[1] = ac % M; ac /= M; act[2] = ac; for (int i=0;i<3;i++) res[act[i]]=min(res[act[i]],k); for (int from=0;from<3;from++) { for(int to=0;to<3;to++) { if (from==to) continue; r=min(act[from], cap[to]-act[to]); if (r == 0) continue; for (int w=0;w<3;w++) nact[w]=act[w]; nact[from]-=r; nact[to]+=r; nx = M*M*nact[2] + M*nact[1] + nact[0]; if (S.find(nx) == S.end()) { S.insert(nx); Q[h]=nx; K[h]=k+1; h++; } } } k++; } for (int i=0;i<=cap[2];i++) { printf("%d ", (res[i] == M*2 ? -1 : res[i])); } } |
English