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

using namespace std;

void solve(){
    vector<int> capacity(3);
    vector<int> orangeade(3);

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

    vector<int> result(capacity[2] + 1, -1);
    map<vector<int>, int> distances;
    queue<vector<int>> que;

    for(int i = 0; i < 3; i++) result[orangeade[i]] = 0;

    distances[orangeade] = 0;
    que.push(orangeade);
    
    while(!que.empty()) {
        vector<int> current = que.front();
        que.pop();

        vector<int> next(3);
        for(int i = 0; i < 3; i++) {
            for(int j = 0; j < 3; j++) {
                if(i != j) {
                    next = current;
                    int moved = current[i] <= capacity[j] - current[j] ? current[i] : capacity[j] - current[j];
                    next[i] -= moved;
                    next[j] += moved;
                    if(distances.count(next) == 0) {
                        distances[next] = distances[current] + 1;
                        que.push(next);
                        if(result[next[i]] == -1) result[next[i]] = distances[next];
                        if(result[next[j]] == -1) result[next[j]] = distances[next];
                    }
                }
            }   
        }
    }

    for(auto i : result) cout << i << " ";
    cout << "\n";
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    solve();

    return 0;
}