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
// #include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
using namespace std;

struct calc
{
    int a;
    int b;
    int c;
    int i;
};

int hauMacz(int from, int to, int toMax)
{
    return min((toMax - to), from);
}

int main()
{
    FAST int a, b, c;
    int aMax, bMax, cMax;
    unordered_map<int, unordered_map<int, unordered_map<int, int>>> checked;
    cin >> aMax >> bMax >> cMax;
    cin >> a >> b >> c;
    vector<int> xd(cMax + 1, -1);

    queue<calc> q;
    q.push(calc{a, b, c, 0});
    while (q.size())
    {
        calc c = q.front();
        q.pop();
        int tmp = checked[c.a][c.b][c.c];
        if (tmp && tmp <= (c.i + 1))
            continue;

        checked[c.a][c.b][c.c] = c.i + 1;
        if (xd[c.a] == -1 || xd[c.a] > c.i)
            xd[c.a] = c.i;

        if (xd[c.b] == -1 || xd[c.b] > c.i)
            xd[c.b] = c.i;

        if (xd[c.c] == -1 || xd[c.c] > c.i)
            xd[c.c] = c.i;

        //  a ->b
        tmp = hauMacz(c.a, c.b, bMax);
        q.push(calc{c.a - tmp, c.b + tmp, c.c, c.i + 1});
        //  a ->c
        tmp = hauMacz(c.a, c.c, cMax);
        q.push(calc{c.a - tmp, c.b, c.c + tmp, c.i + 1});
        //  b ->a
        tmp = hauMacz(c.b, c.a, aMax);
        q.push(calc{c.a + tmp, c.b - tmp, c.c, c.i + 1});
        //  b->c
        tmp = hauMacz(c.b, c.c, cMax);
        q.push(calc{c.a, c.b - tmp, c.c + tmp, c.i + 1});
        //  c-> a
        tmp = hauMacz(c.c, c.a, aMax);
        q.push(calc{c.a + tmp, c.b, c.c - tmp, c.i + 1});
        //  c-> b
        tmp = hauMacz(c.c, c.b, bMax);
        q.push(calc{c.a, c.b + tmp, c.c - tmp, c.i + 1});
    }

    for (int i = 0; i < xd.size(); i++)
    {
        cout << xd[i] << " ";
    }
    cout << endl;
}