// Krzysztof Baranski (2021.12.10) #include <cstdio> #include <queue> #include <utility> #include <unordered_set> #include <unordered_map> using namespace std; #define ENCODE_ABC(a,b,c) ((((long long)(a))<<40)|(((long long)(b))<<20)|((long long)(c))) #define DECODE_A(abc) ((int)((abc)>>40)) #define DECODE_B(abc) ((int)(((abc)>>20)&0xFFFFF)) #define DECODE_C(abc) ((int)((abc)&0xFFFFF)) #define MAX(a,b) (((a)>(b))?(a):(b)) #define MIN(a,b) (((a)<(b))?(a):(b)) int A, B, C, a, b, c; inline long long AtoB(int ax, int bx, int cx) { return ENCODE_ABC(MAX(0,ax+bx-B), MIN(ax+bx,B), cx); } inline long long AtoC(int ax, int bx, int cx) { return ENCODE_ABC(MAX(0,ax+cx-C), bx, MIN(ax+cx,C)); } inline long long BtoC(int ax, int bx, int cx) { return ENCODE_ABC(ax, MAX(0,bx+cx-C), MIN(bx+cx,C)); } inline long long BtoA(int ax, int bx, int cx) { return ENCODE_ABC(MIN(bx+ax,A), MAX(0,bx+ax-A), cx); } inline long long CtoA(int ax, int bx, int cx) { return ENCODE_ABC(MIN(cx+ax,A), bx, MAX(0,cx+ax-A)); } inline long long CtoB(int ax, int bx, int cx) { return ENCODE_ABC(ax, MIN(cx+bx,B), MAX(0,cx+bx-B)); } long long (*pours[])(int, int, int) = { AtoB, AtoC, BtoA, BtoC, CtoA, CtoB }; int main() { scanf("%d %d %d\n", &A, &B, &C); scanf("%d %d %d\n", &a, &b, &c); unordered_map<int, int> decants; unordered_set<long long> visited; queue<pair<long long,int> > Q; Q.push(make_pair(ENCODE_ABC(a,b,c), 0)); while(!Q.empty()) { long long abc = Q.front().first; int n = Q.front().second; Q.pop(); visited.insert(abc); int ax = DECODE_A(abc); int bx = DECODE_B(abc); int cx = DECODE_C(abc); if(!decants.count(ax)) decants[ax] = n; if(!decants.count(bx)) decants[bx] = n; if(!decants.count(cx)) decants[cx] = n; for(int i=0; i<6; ++i) { long long p = pours[i](ax,bx,cx); if(!visited.count(p)) Q.push(make_pair(p, n+1)); } } for(int i=0; i<=C; ++i) { if(decants.count(i)) printf("%d ", decants[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 | // Krzysztof Baranski (2021.12.10) #include <cstdio> #include <queue> #include <utility> #include <unordered_set> #include <unordered_map> using namespace std; #define ENCODE_ABC(a,b,c) ((((long long)(a))<<40)|(((long long)(b))<<20)|((long long)(c))) #define DECODE_A(abc) ((int)((abc)>>40)) #define DECODE_B(abc) ((int)(((abc)>>20)&0xFFFFF)) #define DECODE_C(abc) ((int)((abc)&0xFFFFF)) #define MAX(a,b) (((a)>(b))?(a):(b)) #define MIN(a,b) (((a)<(b))?(a):(b)) int A, B, C, a, b, c; inline long long AtoB(int ax, int bx, int cx) { return ENCODE_ABC(MAX(0,ax+bx-B), MIN(ax+bx,B), cx); } inline long long AtoC(int ax, int bx, int cx) { return ENCODE_ABC(MAX(0,ax+cx-C), bx, MIN(ax+cx,C)); } inline long long BtoC(int ax, int bx, int cx) { return ENCODE_ABC(ax, MAX(0,bx+cx-C), MIN(bx+cx,C)); } inline long long BtoA(int ax, int bx, int cx) { return ENCODE_ABC(MIN(bx+ax,A), MAX(0,bx+ax-A), cx); } inline long long CtoA(int ax, int bx, int cx) { return ENCODE_ABC(MIN(cx+ax,A), bx, MAX(0,cx+ax-A)); } inline long long CtoB(int ax, int bx, int cx) { return ENCODE_ABC(ax, MIN(cx+bx,B), MAX(0,cx+bx-B)); } long long (*pours[])(int, int, int) = { AtoB, AtoC, BtoA, BtoC, CtoA, CtoB }; int main() { scanf("%d %d %d\n", &A, &B, &C); scanf("%d %d %d\n", &a, &b, &c); unordered_map<int, int> decants; unordered_set<long long> visited; queue<pair<long long,int> > Q; Q.push(make_pair(ENCODE_ABC(a,b,c), 0)); while(!Q.empty()) { long long abc = Q.front().first; int n = Q.front().second; Q.pop(); visited.insert(abc); int ax = DECODE_A(abc); int bx = DECODE_B(abc); int cx = DECODE_C(abc); if(!decants.count(ax)) decants[ax] = n; if(!decants.count(bx)) decants[bx] = n; if(!decants.count(cx)) decants[cx] = n; for(int i=0; i<6; ++i) { long long p = pours[i](ax,bx,cx); if(!visited.count(p)) Q.push(make_pair(p, n+1)); } } for(int i=0; i<=C; ++i) { if(decants.count(i)) printf("%d ", decants[i]); else printf("-1 "); } printf("\n"); return 0; } |