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
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
//  Created by Michal Kowalski on 10/12/2021.
//

#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <utility>

int S[10002];

int A,B,C;
int a,b,c;

int LIMIT_M = 5;

struct TupleHash {
    static_assert(sizeof(int) * 2 == sizeof(size_t));

    size_t operator()(std::tuple<int, int, int, char> t) const noexcept {
        int a = std::get<0>(t);
        int b = std::get<1>(t);
        int c = std::get<2>(t);
        int v = std::get<3>(t);
        unsigned long hash = std::hash<int>()(a) ^ std::hash<int>()(b);
        int cv = (c << 8) | v;
        size_t r = (size_t(hash) << 32) | cv ;
        return r;
    }
};

std::unordered_map<std::tuple<int, int, int, char>,bool,TupleHash> V;

//A => A -> B
//B => A -> C
//C => B -> C
//D => B -> A
//E => C -> A
//F => C -> B

long long key(int _a, int _b, int _c) {
    long long x = _a;
    x = (x << 20) | _b;
    x = (x << 20) | _c;
    return x;
}

inline void check(int a, int b, int c, int m) {
    S[a] = std::min(S[a],m);
    S[b] = std::min(S[b],m);
    S[c] = std::min(S[c],m);
}

bool visited(int a, int b, int c, char v) {
    std::tuple<int, int, int, char> k = {a,b,c,v};
    if (V.find(k) == V.end()) {
        V[k] = true;
        return false;
    } else {
        return true;
    }
}

int main(int argc, const char * argv[]) {
    scanf("%d %d %d",&A,&B,&C);
    scanf("%d %d %d",&a,&b,&c);
    for (int x=0;x<=C;++x) S[x] = C;
    LIMIT_M = C/2+5;
    std::deque<std::tuple<int, int, int, char, int>> Q;
    Q.push_front({a,b,c,'s',0});
    while(true && !Q.empty()) {
        auto q = Q.back();Q.pop_back();
        int a1 = std::get<0>(q);
        int b1 = std::get<1>(q);
        int c1 = std::get<2>(q);
        char v = std::get<3>(q);
        int m = std::get<4>(q);
        if (visited(a1, b1, c1, v)) {
            continue;
        }
        if (v == 's') {
            Q.push_front({a1,b1,c1,'A',m+1});
            Q.push_front({a1,b1,c1,'B',m+1});
            Q.push_front({a1,b1,c1,'C',m+1});
            Q.push_front({a1,b1,c1,'D',m+1});
            Q.push_front({a1,b1,c1,'E',m+1});
            Q.push_front({a1,b1,c1,'F',m+1});
        } else if (v == 'A') {
            int r = B - b1;
            int l = std::min(r,a1);
            a1 = a1 - l;
            b1 = b1 + l;
            if (l > 0) {
                Q.push_front({a1,b1,c1,'A',m+1});
                Q.push_front({a1,b1,c1,'B',m+1});
                Q.push_front({a1,b1,c1,'C',m+1});
                //Q.push_front({a1,b1,c1,'D',m+1});
                Q.push_front({a1,b1,c1,'E',m+1});
                Q.push_front({a1,b1,c1,'F',m+1});
            }
        }  else if (v == 'B') {
            int r = C - c1;
            int l = std::min(r,a1);
            a1 = a1 - l;
            c1 = c1 + l;
            if (l > 0) {
                Q.push_front({a1,b1,c1,'A',m+1});
                Q.push_front({a1,b1,c1,'B',m+1});
                Q.push_front({a1,b1,c1,'C',m+1});
                Q.push_front({a1,b1,c1,'D',m+1});
                //Q.push_front({a1,b1,c1,'E',m+1});
                Q.push_front({a1,b1,c1,'F',m+1});
            }
        } else if (v == 'C') {
            int r = C - c1;
            int l = std::min(r,b1);
            b1 = b1 - l;
            c1 = c1 + l;
            if (l > 0) {
                Q.push_front({a1,b1,c1,'A',m+1});
                Q.push_front({a1,b1,c1,'B',m+1});
                Q.push_front({a1,b1,c1,'C',m+1});
                Q.push_front({a1,b1,c1,'D',m+1});
                Q.push_front({a1,b1,c1,'E',m+1});
                //Q.push_front({a1,b1,c1,'F',m+1});
            }
        } else if (v == 'D') {
            int r = A - a1;
            int l = std::min(r,b1);
            b1 = b1 - l;
            a1 = a1 + l;
            if (l > 0) {
                //Q.push_front({a1,b1,c1,'A',m+1});
                Q.push_front({a1,b1,c1,'B',m+1});
                Q.push_front({a1,b1,c1,'C',m+1});
                Q.push_front({a1,b1,c1,'D',m+1});
                Q.push_front({a1,b1,c1,'E',m+1});
                Q.push_front({a1,b1,c1,'F',m+1});
            }
        } else if (v == 'E') {
            int r = A - a1;
            int l = std::min(r,c1);
            c1 = c1 - l;
            a1 = a1 + l;
            if (l > 0) {
                Q.push_front({a1,b1,c1,'A',m+1});
                //Q.push_front({a1,b1,c1,'B',m+1});
                Q.push_front({a1,b1,c1,'C',m+1});
                Q.push_front({a1,b1,c1,'D',m+1});
                Q.push_front({a1,b1,c1,'E',m+1});
                Q.push_front({a1,b1,c1,'F',m+1});
            }
        } else if (v == 'F') {
            int r = B - b1;
            int l = std::min(r,c1);
            c1 = c1 - l;
            b1 = b1 + l;
            if (l > 0) {
                Q.push_front({a1,b1,c1,'A',m+1});
                Q.push_front({a1,b1,c1,'B',m+1});
                //Q.push_front({a1,b1,c1,'C',m+1});
                Q.push_front({a1,b1,c1,'D',m+1});
                Q.push_front({a1,b1,c1,'E',m+1});
                Q.push_front({a1,b1,c1,'F',m+1});
            }
        }
        //printf("Q.size: %d\n",Q.size());
        check(a1, b1, c1, m);
        if (m > LIMIT_M) break;
    }
    for (int i = 0; i <= C; ++i) printf("%d ",S[i] == C ? -1 : S[i]);
    printf("\n");
    return 0;
}