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
#include<iostream>
#include<vector>
#include<algorithm>
#include<deque>
#include<unordered_set>

using namespace std;

int main(){
    int a, b, c, A, B, C;
    cin >> A >> B >> C >> a >> b >> c;
    vector<int> v(C+1);
    deque<pair<pair<int,int>, pair<int,int>>> q;
    q.push_back(make_pair(make_pair(a, b), make_pair(c,0)));
    for(int i=0;i<C+1;i++){
        v[i] = 1000000000;
    }
    int time = 0;
    unordered_set<long long> seen;
    while(!q.empty()){
        auto aa = q.front();
        q.pop_front();
        a = aa.first.first;
        b = aa.first.second;
        c = aa.second.first;
        time = aa.second.second;
        v[a] = min(time, v[a]);
        v[b] = min(time, v[b]);
        v[c] = min(time, v[c]);
        long long longa = a;
        long long longb = b;
        long long longc = c;
        if(seen.find(10000000000*longa+100000*longb+longc)!=seen.end()){
            continue;
        }else{
            seen.insert(10000000000*longa+100000*longb+longc);
        }
        int new_a, new_b, new_c;
        new_a=max(0, a-(B-b));
        new_b=b+(a-new_a);
        if(new_a!=a&&new_b!=b){
            q.push_back(make_pair(make_pair(new_a, new_b), make_pair(c, time+1)));
        }

        new_b=max(0, b-(A-a));
        new_a=a+(b-new_b);
        if(new_a!=a&&new_b!=b){
            q.push_back(make_pair(make_pair(new_a, new_b), make_pair(c, time+1)));
        }

        new_c=max(0, c-(A-a));
        new_a=a+(c-new_c);
        if(new_a!=a&&new_c!=c){
            q.push_back(make_pair(make_pair(new_a, b), make_pair(new_c, time+1)));
        }
        new_a=max(0, a-(C-c));
        new_c=c+(a-new_a);
        if(new_a!=a&&new_c!=c){
            q.push_back(make_pair(make_pair(new_a, b), make_pair(new_c, time+1)));
        }
        new_b=max(0, b-(C-c));
        new_c=c+(b-new_b);
        if(new_c!=c&&new_b!=b){
            q.push_back(make_pair(make_pair(a, new_b), make_pair(new_c, time+1)));
        }
        new_c=max(0, c-(B-b));
        new_b=b+(c-new_c);
        if(new_c!=c&&new_b!=b){
            q.push_back(make_pair(make_pair(a, new_b), make_pair(new_c, time+1)));
        }
    }

    for(int i=0;i<C+1;i++){
        cout << (v[i] < 999999999 ? v[i] : -1) << " ";
    }
    cout << endl;
}