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
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
#include <functional>
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

struct State
{
	vector<int> values;
	int result;
};

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

	vector<int> sizes(3);

	for(auto& size : sizes)
	{
		cin >> size;
	}

	vector<int> values(3);

	for(auto& value : values)
	{
		cin >> value;
	}

	vector<vector<bool>> empty(3, vector<bool>(sizes[2] + 1)), full(3, vector<bool>(sizes[2] + 1));
	vector<int> results(sizes[2] + 1, -1);
	queue<State> states;

	states.push({ values, 0 });

	while(!states.empty())
	{
		vector<int> values = states.front().values;
		int result = states.front().result;
//cout << "[STATE] (" << values[0] << ' ' << values[1] << ' ' << values[2] << ") IN " << result << endl;
		states.pop();

		for(int index = 0; index <= 2; index++)
		{
			if(results[values[index]] == -1)
			{
				results[values[index]] = result;
			}
		}

		for(int source = 0; source <= 2; source++)
		{
			for(int destination = 0; destination <= 2; destination++)
			{
				if(source == destination)
				{
					continue;
				}

				if(values[source] + values[destination] >= sizes[destination])
				{
					vector<int> newValues = values;
					
					newValues[destination] = sizes[destination];
					newValues[source] += values[destination] - sizes[destination];
//cout << source << " -> " << destination << " (" << newValues[0] << ' ' << newValues[1] << ' ' << newValues[2] << ")" << endl;
					int value = newValues[(destination + 1) % 3];
		
					if(!full[destination][value])
					{
						full[destination][value] = true;
						states.push({ newValues, result + 1 });
					}
				}
				else
				{
					vector<int> newValues = values;
					
					newValues[destination] += values[source];
					newValues[source] = 0;
		
//cout << source << " -> " << destination << " (" << newValues[0] << ' ' << newValues[1] << ' ' << newValues[2] << ")" << endl;
					int value = newValues[(source + 1) % 3];
		
					if(!empty[source][value])
					{
						empty[source][value] = true;
						states.push({ newValues, result + 1 });
					}
				}
			}
		}
	}

	for(auto value : results)
	{
		cout << value << ' ';
	}

	cout << '\n';
}