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
78
79
80
81
82
83
84
85
86
87
88
89
#include <queue>
#include <stdio.h>
#include <unordered_map>

struct Solution
{
	int a;
	int b;
	int c;

	bool operator==(const Solution &s) const
	{
		return a == s.a && b == s.b && c == s.c;
	}

	struct HashFunction
	{
		size_t operator()(const Solution &s) const
		{
			constexpr auto MAX_N = 10000000;
			size_t a = std::hash<int>()(s.a);
			size_t b = std::hash<int>()(s.b);
			size_t c = std::hash<int>()(s.c);
			return a * MAX_N * MAX_N + b * MAX_N + c;
		}
	};

	std::vector<Solution> generate(const int &A, const int &B, const int &C) const
	{
		std::vector<Solution> solutions;
		solutions.push_back({a - std::min(a, B - b), b + std::min(a, B - b), c}); //a->b
		solutions.push_back({a - std::min(a, C - c), b, c + std::min(a, C - c)}); //a->c

		solutions.push_back({a + std::min(b, A - a), b - std::min(b, A - a), c}); //b->a
		solutions.push_back({a, b - std::min(b, C - c), c + std::min(b, C - c)}); //b->c

		solutions.push_back({a + std::min(c, A - a), b, c - std::min(c, A - a)}); //c->a
		solutions.push_back({a, b + std::min(c, B - b), c - std::min(c, B - b)}); //c->b

		return solutions;
	}
};

void solve(const int &A, const int &B, const int &C, const Solution initSolution)
{
	std::queue<Solution> solutions;
	std::unordered_map<Solution, int, Solution::HashFunction> moves;
	std::unordered_map<int, int> minimalMoves;
	solutions.push(initSolution);
	moves[initSolution] = 0;
	while (!solutions.empty())
	{
		const auto &s = solutions.front();
		const auto m = moves[s];
		if (minimalMoves.count(s.a) == 0)
			minimalMoves[s.a] = m;
		if (minimalMoves.count(s.b) == 0)
			minimalMoves[s.b] = m;
		if (minimalMoves.count(s.c) == 0)
			minimalMoves[s.c] = m;

		for (const auto &g : s.generate(A, B, C))
		{
			if (moves.count(g) == 0)
			{
				moves[g] = m + 1;
				solutions.push(g);
			}
		}
		solutions.pop();
	}
	for (int i = 0; i <= C; ++i)
	{
		if (minimalMoves.count(i))
			printf("%d ", minimalMoves[i]);
		else
			printf("-1 ");
	}
	printf("\n");
}

int main()
{
	int A, B, C, a, b, c;
	scanf("%d %d %d\n", &A, &B, &C);
	scanf("%d %d %d\n", &a, &b, &c);
	solve(A, B, C, {a, b, c});
	return 0;
}