#include <stdio.h> #include <unordered_map> #define Inf 1000000000 struct State { int l[3]; int step; }; int Results[200000]; int C[3]; State Queue[200000]; int From; int To; struct StateComp { int operator()(State s) const { return (1 + s.l[0]) * (1 + s.l[1]) * (1 + s.l[2]) % 1'000'000'007; } bool operator()(State s1, State s2) const { return s1.l[0] == s2.l[0] && s1.l[1] == s2.l[1] && s1.l[2] == s2.l[2]; } }; std::unordered_map<State, int, StateComp, StateComp> map; int getHashMapStep(State state) { if (map.find(state) == map.end()) return Inf; return map[state]; } int min(int x, int y) { return x < y ? x : y; } int c; void tryPush(State state) { int step = state.step; if( step < getHashMapStep(state) ) { Queue[To] = state; To++; Results[state.l[0]] = min(Results[state.l[0]], step); Results[state.l[1]] = min(Results[state.l[1]], step); Results[state.l[2]] = min(Results[state.l[2]], step); map[state] = step; } } State pop() { From++; return Queue[From - 1]; } State Pour(State state, int from, int to) { int free = C[to] - state.l[to]; int amount = min(free, state.l[from]); State newState = state; newState.step = state.step + 1; newState.l[from] -= amount; newState.l[to] += amount; return newState; } int main() { State initState; initState.step = 0; scanf("%d ", &C[0]); scanf("%d ", &C[1]); scanf("%d ", &C[2]); scanf("%d ", &initState.l[0]); scanf("%d ", &initState.l[1]); scanf("%d ", &initState.l[2]); for(int i=0; i <= C[2]; i++) Results[i] = Inf; tryPush(initState); while(From < To) { State state = pop(); tryPush(Pour(state, 0, 1)); tryPush(Pour(state, 0, 2)); tryPush(Pour(state, 1, 0)); tryPush(Pour(state, 1, 2)); tryPush(Pour(state, 2, 0)); tryPush(Pour(state, 2, 1)); } for(int i = 0; i <= C[2]; i++) printf("%d ", Results[i] == Inf ? -1 : Results[i] ); printf("\n"); 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 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 | #include <stdio.h> #include <unordered_map> #define Inf 1000000000 struct State { int l[3]; int step; }; int Results[200000]; int C[3]; State Queue[200000]; int From; int To; struct StateComp { int operator()(State s) const { return (1 + s.l[0]) * (1 + s.l[1]) * (1 + s.l[2]) % 1'000'000'007; } bool operator()(State s1, State s2) const { return s1.l[0] == s2.l[0] && s1.l[1] == s2.l[1] && s1.l[2] == s2.l[2]; } }; std::unordered_map<State, int, StateComp, StateComp> map; int getHashMapStep(State state) { if (map.find(state) == map.end()) return Inf; return map[state]; } int min(int x, int y) { return x < y ? x : y; } int c; void tryPush(State state) { int step = state.step; if( step < getHashMapStep(state) ) { Queue[To] = state; To++; Results[state.l[0]] = min(Results[state.l[0]], step); Results[state.l[1]] = min(Results[state.l[1]], step); Results[state.l[2]] = min(Results[state.l[2]], step); map[state] = step; } } State pop() { From++; return Queue[From - 1]; } State Pour(State state, int from, int to) { int free = C[to] - state.l[to]; int amount = min(free, state.l[from]); State newState = state; newState.step = state.step + 1; newState.l[from] -= amount; newState.l[to] += amount; return newState; } int main() { State initState; initState.step = 0; scanf("%d ", &C[0]); scanf("%d ", &C[1]); scanf("%d ", &C[2]); scanf("%d ", &initState.l[0]); scanf("%d ", &initState.l[1]); scanf("%d ", &initState.l[2]); for(int i=0; i <= C[2]; i++) Results[i] = Inf; tryPush(initState); while(From < To) { State state = pop(); tryPush(Pour(state, 0, 1)); tryPush(Pour(state, 0, 2)); tryPush(Pour(state, 1, 0)); tryPush(Pour(state, 1, 2)); tryPush(Pour(state, 2, 0)); tryPush(Pour(state, 2, 1)); } for(int i = 0; i <= C[2]; i++) printf("%d ", Results[i] == Inf ? -1 : Results[i] ); printf("\n"); return 0; } |