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
#include<bits/stdc++.h>
using namespace std;
#define st first
#define nd second
const int INF = 1e9;

int32_t main(){
    ios::sync_with_stdio(false);
    vector<int> poj(3), tab(3);
    for(int i=0;i<3;i++)
        cin >> poj[i];
    int C = poj.back();

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

    auto pour = [&](vector<int> a, int i, int j) {
        assert(i != j);
        vector<int> res = a;
        int d = min(a[i], poj[j]-a[j]);
        res[i] -= d;
        res[j] += d;
        return res;
    };

    map<vector<int>, int> M;
    queue<vector<int> > q;
    M[tab] = 0;
    q.push(tab);
    while(!q.empty()) {
        auto v = q.front();
        q.pop();
        for(int i=0;i<3;i++)
            for(int j=0;j<3;j++)
                if(i != j) {
                    auto u = pour(v,i,j);
                    if(M.count(u)) continue;
                    M[u] = M[v]+1;
                    q.push(u);
                }
    }
    vector<int> ans(C+1,INF);
    for(auto x:M)
        for(auto y:x.st)
            ans[y] = min(ans[y], x.nd);
    for(auto a:ans)
        cout<<(a == INF ? -1 : a)<<" ";
    cout<<"\n";

}