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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 *  Copyright (C) 2021  Paweł Widera
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details:
 *  http://www.gnu.org/licenses/gpl.html
 */
#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <unordered_map>
using namespace std;

#define MOD 4294967295


unsigned int SIZES[3];

struct State {
	unsigned int a;
	unsigned int b;
	unsigned int c;

	unsigned int& operator[](int i) {
		return (i == 0) ? a : (i == 1) ? b : c;
	}

	bool operator==(const State& other) const {
		return a == other.a && b == other.b && c == other.c;
	}
};


namespace std {
	template <> struct hash<State> {
		inline size_t operator()(const State& s) const {
			// universal hashing with random multipliers
			return ((s.a * 81335) + (s.b * 218100) + (s.c * 199972) + 112524) % MOD;
		}
    };
}


void generate_next(State& current, vector<State>& neighbours) {
	for (int i = 0; i < 3; ++i) {
		for (int j = 0; j < 3; ++j) {
			// unless source is empty or destination is full
			if (i != j || current[i] > 0 || current[j] < SIZES[j]) {
				// transfer a volume of liquid that fits to destination
				int volume = min(current[i], SIZES[j] - current[j]);
				State next = current;
				next[j] += volume;
				next[i] -= volume;
				neighbours.emplace_back(next);
			}
		}
	}
}


int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	for (int i = 0; i < 3; ++i) {
		cin >> SIZES[i];
	}

	State start;
	cin >> start.a >> start.b >> start.c;

	vector<int> moves(SIZES[2] + 1, -1);
	moves[start.a] = 0;
	moves[start.b] = 0;
	moves[start.c] = 0;

	deque<State> queue;
	vector<State> neighbours;
	unordered_map<State, int> distances;

	queue.emplace_back(start);
	distances[start] = 0;

	// try all moves, and track the obtained volumes
	while (!queue.empty()) {
		auto current = queue.front();
		queue.pop_front();

		generate_next(current, neighbours);

		while (!neighbours.empty()) {
			auto state = neighbours.back();
			neighbours.pop_back();

			if (!distances.count(state)) {
				// add to queue if state not visited yet
				queue.emplace_back(state);
				// update moves
				int d = distances[current] + 1;
				if (moves[state.a] < 0) moves[state.a] = d;
				if (moves[state.b] < 0) moves[state.b] = d;
				if (moves[state.c] < 0) moves[state.c] = d;
				// update distance
				distances[state] = d;
			}
		}
	}

	for (auto m: moves) {
		cout << m << " ";
	}
	cout << endl;
	return 0;
}