#include<bits/stdc++.h>
#define maxn 100010
#define inf 1000000007
using namespace std;
int ile[maxn];
map<pair<int,int>, bool> czy;
queue<pair<pair<int,int>, int> > prl;
int get_miejsce(int A, int B, int C, int a, int b, int c, int x) {
if (x == 0)
return A-a;
else if (x == 1)
return B-b;
else
return C-c;
}
pair<int,int> przelej(int A, int B, int C, int suma, int a, int b, int i, int j) {
int miejsce = get_miejsce(A,B,C, a,b,suma-a-b,j);
int c = suma-a-b;
if (i == 0) {
miejsce = min(miejsce, a);
if (j == 1)
return make_pair(a-miejsce, b+miejsce);
else
return make_pair(a-miejsce, b);
}
else if (i == 1) {
miejsce = min(miejsce, b);
if (j == 0)
return make_pair(a+miejsce, b-miejsce);
else
return make_pair(a, b-miejsce);
}
else {
miejsce = min(miejsce, c);
if (j == 0)
return make_pair(a+miejsce, b);
else
return make_pair(a, b+miejsce);
}
}
void update(pair<int,int> x, int suma, int y) {
ile[x.first] = min(ile[x.first], y);
ile[x.second] = min(ile[x.second], y);
ile[suma-x.first-x.second] = min(ile[suma-x.first-x.second], y);
}
void bfs(int A, int B, int C, int suma) {
while(!prl.empty()) {
pair<pair<int,int>, int> x = prl.front();
prl.pop();
for (int i = 0; i <3; ++i) {
for (int j = 0; j < 3; ++j) {
if (i == j)
continue;
pair<int,int> new_x = przelej(A,B,C,suma, x.first.first, x.first.second, i, j);
if (!czy[new_x]) {
update(new_x, suma, x.second+1);
prl.push(make_pair(new_x, x.second+1));
czy[new_x] = true;
}
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int A, B, C;
cin >> A >> B >> C;
int a, b, c;
cin >> a >> b >> c;
for (int i = 0; i <= C; ++i)
ile[i] = inf;
ile[a] = ile[b] = ile[c] = 0;
czy[{a,b}] = true;
prl.push(make_pair(make_pair(a,b),0));
bfs(A,B,C, a+b+c);
for (int i = 0; i <= C; ++i) {
if (ile[i] != inf)
cout << ile[i] << " ";
else
cout << -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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | #include<bits/stdc++.h> #define maxn 100010 #define inf 1000000007 using namespace std; int ile[maxn]; map<pair<int,int>, bool> czy; queue<pair<pair<int,int>, int> > prl; int get_miejsce(int A, int B, int C, int a, int b, int c, int x) { if (x == 0) return A-a; else if (x == 1) return B-b; else return C-c; } pair<int,int> przelej(int A, int B, int C, int suma, int a, int b, int i, int j) { int miejsce = get_miejsce(A,B,C, a,b,suma-a-b,j); int c = suma-a-b; if (i == 0) { miejsce = min(miejsce, a); if (j == 1) return make_pair(a-miejsce, b+miejsce); else return make_pair(a-miejsce, b); } else if (i == 1) { miejsce = min(miejsce, b); if (j == 0) return make_pair(a+miejsce, b-miejsce); else return make_pair(a, b-miejsce); } else { miejsce = min(miejsce, c); if (j == 0) return make_pair(a+miejsce, b); else return make_pair(a, b+miejsce); } } void update(pair<int,int> x, int suma, int y) { ile[x.first] = min(ile[x.first], y); ile[x.second] = min(ile[x.second], y); ile[suma-x.first-x.second] = min(ile[suma-x.first-x.second], y); } void bfs(int A, int B, int C, int suma) { while(!prl.empty()) { pair<pair<int,int>, int> x = prl.front(); prl.pop(); for (int i = 0; i <3; ++i) { for (int j = 0; j < 3; ++j) { if (i == j) continue; pair<int,int> new_x = przelej(A,B,C,suma, x.first.first, x.first.second, i, j); if (!czy[new_x]) { update(new_x, suma, x.second+1); prl.push(make_pair(new_x, x.second+1)); czy[new_x] = true; } } } } } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int A, B, C; cin >> A >> B >> C; int a, b, c; cin >> a >> b >> c; for (int i = 0; i <= C; ++i) ile[i] = inf; ile[a] = ile[b] = ile[c] = 0; czy[{a,b}] = true; prl.push(make_pair(make_pair(a,b),0)); bfs(A,B,C, a+b+c); for (int i = 0; i <= C; ++i) { if (ile[i] != inf) cout << ile[i] << " "; else cout << -1 << " "; } } |
English