#include <iostream>
#include <array>
#include <set>
#include <queue>
const int N = 100000;
using Levels = std::array<int, 3>;
std::set<Levels> visitedLevels;
int pours[N+1];
int capacities[3];
Levels pour(Levels levels, int from, int to, int other){
int amount = std::min(levels[from], capacities[to] - levels[to]);
Levels ret;
ret[from] = levels[from] - amount;
ret[to] = levels[to] + amount;
ret[other] = levels[other];
return ret;
}
int main(){
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
Levels initLevels;
std::cin >> capacities[0] >> capacities[1] >> capacities[2];
std::cin >> initLevels[0] >> initLevels[1] >> initLevels[2];
std::queue<std::pair<Levels, int>> queue;
queue.push({initLevels, 1});
visitedLevels.insert(initLevels);
for(int level: initLevels){
pours[level] = 1;
}
while(!queue.empty()){
auto [levels, iter] = queue.front();
queue.pop();
for(int i = 0; i < 6; i++){
Levels newLevels = pour(levels, i/2, 2-(i%3), ((i+3)%6)/2);
if(visitedLevels.find(newLevels) == visitedLevels.end()){
for(int level: newLevels){
if(pours[level] == 0){
pours[level] = iter + 1;
}
}
queue.push({newLevels, iter+1});
visitedLevels.insert(newLevels);
}
}
}
for(int i = 0; i <= capacities[2]; i++){
std::cout << pours[i] - 1 << " ";
}
return 0;
}
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 | #include <iostream> #include <array> #include <set> #include <queue> const int N = 100000; using Levels = std::array<int, 3>; std::set<Levels> visitedLevels; int pours[N+1]; int capacities[3]; Levels pour(Levels levels, int from, int to, int other){ int amount = std::min(levels[from], capacities[to] - levels[to]); Levels ret; ret[from] = levels[from] - amount; ret[to] = levels[to] + amount; ret[other] = levels[other]; return ret; } int main(){ std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); Levels initLevels; std::cin >> capacities[0] >> capacities[1] >> capacities[2]; std::cin >> initLevels[0] >> initLevels[1] >> initLevels[2]; std::queue<std::pair<Levels, int>> queue; queue.push({initLevels, 1}); visitedLevels.insert(initLevels); for(int level: initLevels){ pours[level] = 1; } while(!queue.empty()){ auto [levels, iter] = queue.front(); queue.pop(); for(int i = 0; i < 6; i++){ Levels newLevels = pour(levels, i/2, 2-(i%3), ((i+3)%6)/2); if(visitedLevels.find(newLevels) == visitedLevels.end()){ for(int level: newLevels){ if(pours[level] == 0){ pours[level] = iter + 1; } } queue.push({newLevels, iter+1}); visitedLevels.insert(newLevels); } } } for(int i = 0; i <= capacities[2]; i++){ std::cout << pours[i] - 1 << " "; } return 0; } |
English