#include <iostream> #include <unordered_map> #include <vector> constexpr int FOUND_EVERYTHING_CODE = 12345678; constexpr long long TEN_5 = 100000; constexpr long long TEN_4 = 10000; constexpr long long TEN_10 = TEN_5 * TEN_5; constexpr long long MAX_VOLUME = TEN_5; int A; int B; int C; int a; int b; int c; std::unordered_map<long long, bool> map; int numberOfSteps[MAX_VOLUME]; int numberOfStepsFound = 0; void prepareVariables() { for (int i = 0; i < MAX_VOLUME; i++) numberOfSteps[i] = -1; } void readInput() { std::cin >> A >> B >> C >> a >> b >> c; } long long createKey(int first, int second, int third) { return first + TEN_5 * second + TEN_10 * third; } bool hasStateAlready(int first, int second, int third) { return map.find(createKey(first, second, third)) != map.end(); } void addState(int first, int second, int third, int stepsNumber) { if (numberOfSteps[first] == -1) { numberOfSteps[first] = stepsNumber; numberOfStepsFound++; } if (numberOfSteps[second] == -1) { numberOfSteps[second] = stepsNumber; numberOfStepsFound++; } if (numberOfSteps[third] == -1) { numberOfSteps[third] = stepsNumber; numberOfStepsFound++; } map[createKey(first, second, third)] = true; if (numberOfStepsFound == C + 1) throw FOUND_EVERYTHING_CODE; } class State { public: int first; int second; int third; State(int first, int second, int third) : first(first), second(second), third(third) {} }; void search(int first, int second, int third) { int stepsNumber = 0; std::vector<State> statesToSearch; statesToSearch.reserve(TEN_4); std::vector<State>* statesToSearchPtr = &statesToSearch; std::vector<State> nextStatesToSearch; nextStatesToSearch.reserve(TEN_4); std::vector<State>* nextStatesToSearchPtr = &nextStatesToSearch; statesToSearch.emplace_back(first, second, third); addState(first, second, third, stepsNumber); while (statesToSearchPtr->size() != 0) { for (auto state : (*statesToSearchPtr)) { // first to second int howMuchCanGo = std::min(state.first, B - state.second); if (!hasStateAlready(state.first - howMuchCanGo, state.second + howMuchCanGo, state.third)) { addState(state.first - howMuchCanGo, state.second + howMuchCanGo, state.third, stepsNumber + 1); nextStatesToSearchPtr->emplace_back(state.first - howMuchCanGo, state.second + howMuchCanGo, state.third); } // second to first howMuchCanGo = std::min(state.second, A - state.first); if (!hasStateAlready(state.first + howMuchCanGo, state.second - howMuchCanGo, state.third)) { addState(state.first + howMuchCanGo, state.second - howMuchCanGo, state.third, stepsNumber + 1); nextStatesToSearchPtr->emplace_back(state.first + howMuchCanGo, state.second - howMuchCanGo, state.third); } // first to third howMuchCanGo = std::min(state.first, C - state.third); if (!hasStateAlready(state.first - howMuchCanGo, state.second, state.third + howMuchCanGo)) { addState(state.first - howMuchCanGo, state.second, state.third + howMuchCanGo, stepsNumber + 1); nextStatesToSearchPtr->emplace_back(state.first - howMuchCanGo, state.second, state.third + howMuchCanGo); } // third to first howMuchCanGo = std::min(state.third, A - state.first); if (!hasStateAlready(state.first + howMuchCanGo, state.second, state.third - howMuchCanGo)) { addState(state.first + howMuchCanGo, state.second, state.third - howMuchCanGo, stepsNumber + 1); nextStatesToSearchPtr->emplace_back(state.first + howMuchCanGo, state.second, state.third - howMuchCanGo); } // second to third howMuchCanGo = std::min(state.second, C - state.third); if (!hasStateAlready(state.first, state.second - howMuchCanGo, state.third + howMuchCanGo)) { addState(state.first, state.second - howMuchCanGo, state.third + howMuchCanGo, stepsNumber + 1); nextStatesToSearchPtr->emplace_back(state.first, state.second - howMuchCanGo, state.third + howMuchCanGo); } // third to first howMuchCanGo = std::min(state.third, B - state.second); if (!hasStateAlready(state.first, state.second + howMuchCanGo, state.third - howMuchCanGo)) { addState(state.first, state.second + howMuchCanGo, state.third - howMuchCanGo, stepsNumber + 1); nextStatesToSearchPtr->emplace_back(state.first, state.second + howMuchCanGo, state.third - howMuchCanGo); } } stepsNumber++; statesToSearchPtr->clear(); std::vector<State>* tmpPtr = nextStatesToSearchPtr; nextStatesToSearchPtr = statesToSearchPtr; statesToSearchPtr = tmpPtr; } } void writeFindings() { for (int i = 0; i < C + 1; i++) { std::cout << numberOfSteps[i]; if (i != C) std::cout << " "; } } void trySearch() { try { search(a, b, c); writeFindings(); } catch (int e) { if (e == FOUND_EVERYTHING_CODE) writeFindings(); } } void createTestInput(int a_, int b_, int c_, int A_, int B_, int C_) { a = a_; b = b_; c = c_; A = A_; B = B_; C = C_; } void solve() { prepareVariables(); readInput(); trySearch(); } int main() { solve(); 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 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | #include <iostream> #include <unordered_map> #include <vector> constexpr int FOUND_EVERYTHING_CODE = 12345678; constexpr long long TEN_5 = 100000; constexpr long long TEN_4 = 10000; constexpr long long TEN_10 = TEN_5 * TEN_5; constexpr long long MAX_VOLUME = TEN_5; int A; int B; int C; int a; int b; int c; std::unordered_map<long long, bool> map; int numberOfSteps[MAX_VOLUME]; int numberOfStepsFound = 0; void prepareVariables() { for (int i = 0; i < MAX_VOLUME; i++) numberOfSteps[i] = -1; } void readInput() { std::cin >> A >> B >> C >> a >> b >> c; } long long createKey(int first, int second, int third) { return first + TEN_5 * second + TEN_10 * third; } bool hasStateAlready(int first, int second, int third) { return map.find(createKey(first, second, third)) != map.end(); } void addState(int first, int second, int third, int stepsNumber) { if (numberOfSteps[first] == -1) { numberOfSteps[first] = stepsNumber; numberOfStepsFound++; } if (numberOfSteps[second] == -1) { numberOfSteps[second] = stepsNumber; numberOfStepsFound++; } if (numberOfSteps[third] == -1) { numberOfSteps[third] = stepsNumber; numberOfStepsFound++; } map[createKey(first, second, third)] = true; if (numberOfStepsFound == C + 1) throw FOUND_EVERYTHING_CODE; } class State { public: int first; int second; int third; State(int first, int second, int third) : first(first), second(second), third(third) {} }; void search(int first, int second, int third) { int stepsNumber = 0; std::vector<State> statesToSearch; statesToSearch.reserve(TEN_4); std::vector<State>* statesToSearchPtr = &statesToSearch; std::vector<State> nextStatesToSearch; nextStatesToSearch.reserve(TEN_4); std::vector<State>* nextStatesToSearchPtr = &nextStatesToSearch; statesToSearch.emplace_back(first, second, third); addState(first, second, third, stepsNumber); while (statesToSearchPtr->size() != 0) { for (auto state : (*statesToSearchPtr)) { // first to second int howMuchCanGo = std::min(state.first, B - state.second); if (!hasStateAlready(state.first - howMuchCanGo, state.second + howMuchCanGo, state.third)) { addState(state.first - howMuchCanGo, state.second + howMuchCanGo, state.third, stepsNumber + 1); nextStatesToSearchPtr->emplace_back(state.first - howMuchCanGo, state.second + howMuchCanGo, state.third); } // second to first howMuchCanGo = std::min(state.second, A - state.first); if (!hasStateAlready(state.first + howMuchCanGo, state.second - howMuchCanGo, state.third)) { addState(state.first + howMuchCanGo, state.second - howMuchCanGo, state.third, stepsNumber + 1); nextStatesToSearchPtr->emplace_back(state.first + howMuchCanGo, state.second - howMuchCanGo, state.third); } // first to third howMuchCanGo = std::min(state.first, C - state.third); if (!hasStateAlready(state.first - howMuchCanGo, state.second, state.third + howMuchCanGo)) { addState(state.first - howMuchCanGo, state.second, state.third + howMuchCanGo, stepsNumber + 1); nextStatesToSearchPtr->emplace_back(state.first - howMuchCanGo, state.second, state.third + howMuchCanGo); } // third to first howMuchCanGo = std::min(state.third, A - state.first); if (!hasStateAlready(state.first + howMuchCanGo, state.second, state.third - howMuchCanGo)) { addState(state.first + howMuchCanGo, state.second, state.third - howMuchCanGo, stepsNumber + 1); nextStatesToSearchPtr->emplace_back(state.first + howMuchCanGo, state.second, state.third - howMuchCanGo); } // second to third howMuchCanGo = std::min(state.second, C - state.third); if (!hasStateAlready(state.first, state.second - howMuchCanGo, state.third + howMuchCanGo)) { addState(state.first, state.second - howMuchCanGo, state.third + howMuchCanGo, stepsNumber + 1); nextStatesToSearchPtr->emplace_back(state.first, state.second - howMuchCanGo, state.third + howMuchCanGo); } // third to first howMuchCanGo = std::min(state.third, B - state.second); if (!hasStateAlready(state.first, state.second + howMuchCanGo, state.third - howMuchCanGo)) { addState(state.first, state.second + howMuchCanGo, state.third - howMuchCanGo, stepsNumber + 1); nextStatesToSearchPtr->emplace_back(state.first, state.second + howMuchCanGo, state.third - howMuchCanGo); } } stepsNumber++; statesToSearchPtr->clear(); std::vector<State>* tmpPtr = nextStatesToSearchPtr; nextStatesToSearchPtr = statesToSearchPtr; statesToSearchPtr = tmpPtr; } } void writeFindings() { for (int i = 0; i < C + 1; i++) { std::cout << numberOfSteps[i]; if (i != C) std::cout << " "; } } void trySearch() { try { search(a, b, c); writeFindings(); } catch (int e) { if (e == FOUND_EVERYTHING_CODE) writeFindings(); } } void createTestInput(int a_, int b_, int c_, int A_, int B_, int C_) { a = a_; b = b_; c = c_; A = A_; B = B_; C = C_; } void solve() { prepareVariables(); readInput(); trySearch(); } int main() { solve(); return 0; } |