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
//Mateusz Piórkowski
#include <iostream>
#include <set>
#include <queue>
#include <limits>

struct queue_el{
	//int a_lev,b_lev,c_lev;
	int level[3];
	int moves;
};
struct bottle_state{
	int level[3];
};

inline bool operator<(const bottle_state& a, const bottle_state& b){
	for(int i=0; i<3; i++)
		if(a.level[i]!=b.level[i])
			return a.level[i]<b.level[i];
	return 0;
}

std::set<bottle_state> checked;
std::queue<queue_el> to_check;

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

	int a_cap,b_cap,c_cap;
	int a_lev,b_lev,c_lev;
	std::cin >> a_cap>>b_cap>>c_cap;
	std::cin >> a_lev>>b_lev>>c_lev;
	int bottle_cap[3] = {a_cap,b_cap,c_cap};

	int *moves_to_get = new int[c_cap+1];
	for(int i=0; i<c_cap+1; i++) moves_to_get[i]=std::numeric_limits<int>::max();
	to_check.push({{a_lev,b_lev,c_lev},0});
	//checked.insert({current_state.level[0],current_state.level[1],current_state.level[2]});
	checked.insert({a_lev,b_lev,c_lev});
	while(!to_check.empty()){
		queue_el current_state = to_check.front();
		to_check.pop();
		//checked.insert({current_state.level[0],current_state.level[1],current_state.level[2]});
		//std::cout << "Checking " << current_state.level[0]<<","<<current_state.level[1]<<","<<current_state.level[2]<<"\n";
		//Set moves_to_get
		for(int i=0;i<3;i++){
			moves_to_get[current_state.level[i]] = std::min(moves_to_get[current_state.level[i]],current_state.moves);
		}
		//Check next cases
		for(int i=0;i<3;i++){
			for(int j=0;j<3;j++){
				if(i!=j){
					int new_level[3];
					for(int k=0; k<3; k++) new_level[k]=current_state.level[k];
					int change_by = std::min(new_level[i], bottle_cap[j]-new_level[j]);
					new_level[i]-=change_by;
					new_level[j]+=change_by;

					queue_el to_add_queue;
					bottle_state to_add_set;
					for(int k=0; k<3; k++) to_add_queue.level[k] = new_level[k];
					for(int k=0; k<3; k++) to_add_set.level[k] = new_level[k];
					to_add_queue.moves=current_state.moves+1;
					if(checked.count(to_add_set)==0){
						to_check.push(to_add_queue);
						checked.insert(to_add_set);
					}
					//state to_add;
					//to_add.moves=current_state.moves+1;
				}
			}
		}
	}
	for(int i=0; i<c_cap+1; i++){
		if(moves_to_get[i]==std::numeric_limits<int>::max()) std::cout << "-1 ";
		else std::cout << moves_to_get[i] << " ";
	}
	std::cout << "\n";
}