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
#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int INF = 3*2*100'002;

inline ll vec_to_ll (vector<int> &v) {
    return v[0] + (((ll) v[1] + ((ll) v[2] << 20)) << 20);
}

int main() {
    vector<int> vol(3);
    vector<int> start(3);

    for (int i=0; i<3; ++i)
        cin >> vol[i];

    for (int i=0; i<3; ++i)
        cin >> start[i];

    queue<pair<vector<int>, int>> q;
    unordered_set<ll> visited;

    vector<int> num_steps (vol[2]+1, INF);

    q.push({start, 0});
    visited.insert(vec_to_ll(start));

    while (!q.empty()) {
        auto [v, steps] = q.front();
        for (auto x : v)
            num_steps[x] = min(num_steps[x], steps);
        q.pop();
        for (int i=0; i<3; ++i)
            for (int j=0; j<3; ++j)
                if (i != j) {
                    // przelewamy i -> j
                    vector<int> w = v;
                    int x = min(v[i], vol[j] - v[j]);
                    w[j] += x;
                    w[i] -= x;
                    if (visited.count(vec_to_ll(w)) == 0) {
                        visited.insert(vec_to_ll(w));
                        q.push({w, steps+1});
                    }
                }
    }

    for (auto i : num_steps)
        cout << (i < INF ? i : -1 ) << " ";

    return 0;
}