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
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

int num_of_steps_to_reach[100005];

vector<pair<int, int>> moves = { {0, 1}, {0, 2}, {1, 2}, {1, 0}, {2, 0}, {2, 1} };

int main() {
    int a, b, c, n, abc[3], ABC[3], new_abc[3], max_ABC;

    cin >> ABC[0] >> ABC[1] >> ABC[2];

    max_ABC = max(max(ABC[0], ABC[1]), ABC[2]);

    for(auto i = 0; i <= max_ABC; i++) {
        num_of_steps_to_reach[i] = -1;
    }

    cin >> a >> b >> c;

    num_of_steps_to_reach[a] = 0;
    num_of_steps_to_reach[b] = 0;
    num_of_steps_to_reach[c] = 0;

    n = a + b + c;
    queue<pair<pair<int, int>, int>> q;
    q.push(pair(pair(a, b), 0));


    pair<pair<int, int>, int> pos_decants;
    pair<int, int> aux_pair;
    map<pair<int, int>, bool> odw;
    odw[pair(a, b)] = true;

    int free_space;

    while(!q.empty()) {
        pos_decants = q.front();
        abc[0] = pos_decants.first.first;
        abc[1] = pos_decants.first.second;
        abc[2] = n - abc[0] - abc[1];

        for (auto move: moves) {
            free_space = ABC[move.second] - abc[move.second];

            for(auto i = 0; i < 3; i++) {
                new_abc[i] = abc[i];
            }

            if (abc[move.first] > free_space) {
                new_abc[move.first] -= free_space;
                new_abc[move.second] += free_space;
            }
            else {
                new_abc[move.second] += new_abc[move.first];
                new_abc[move.first] = 0;
            }

            aux_pair = pair(new_abc[0], new_abc[1]);

            if (odw.find(aux_pair) == odw.end()) {
                q.push(pair(aux_pair, pos_decants.second + 1));
                odw[aux_pair] = true;

                for(auto i = 0; i < 3; i++) {
                    if (num_of_steps_to_reach[new_abc[i]] == -1) {
                        num_of_steps_to_reach[new_abc[i]] = pos_decants.second + 1;
                    }
                }
            }
        }

        q.pop();
    }

    for (auto i = 0; i <= ABC[2]; i++) {
        cout << num_of_steps_to_reach[i] << " ";
    }

    return 0;
}