#include <iostream> #include <cmath> using namespace std; unsigned int volumeA, volumeB, volumeC; unsigned int fillingAInitial, fillingBInitial, fillingCInitial; unsigned int fillingACurrent, fillingBCurrent, fillingCCurrent; const char POUR_AB = '0'; const char POUR_AC = '1'; const char POUR_BA = '2'; const char POUR_BC = '3'; const char POUR_CA = '4'; const char POUR_CB = '5'; const string POURS_ALL_POSSIBILITIES = "012345"; const unsigned int NUMBER_OF_POURS_VARIANTS = 6; const unsigned int MAX_POURS = 5; string itoaCustom(int value, int base) { static char buffer[MAX_POURS] = {'0'}; int i = MAX_POURS - 1; for(; value && i; --i, value /= base) { buffer[i] = POURS_ALL_POSSIBILITIES[value % base]; } return buffer; } bool isPouringComplete(unsigned int fillingFinal) { return fillingFinal == fillingACurrent || fillingFinal == fillingBCurrent || fillingFinal == fillingCCurrent; } void finalizePour(unsigned int &fillingFrom, unsigned int &fillingTo, unsigned int volumeTo) { if(volumeTo - fillingTo > fillingFrom) { fillingTo += fillingFrom; fillingFrom = 0; } else { fillingFrom -= (volumeTo - fillingTo); fillingTo = volumeTo; } } #pragma clang diagnostic push #pragma ide diagnostic ignored "hicpp-multiway-paths-covered" void pour(char typePour) { switch(typePour) { case POUR_AB: finalizePour(fillingACurrent, fillingBCurrent, volumeB); break; case POUR_AC: finalizePour(fillingACurrent, fillingCCurrent, volumeC); break; case POUR_BA: finalizePour(fillingBCurrent, fillingACurrent, volumeA); break; case POUR_BC: finalizePour(fillingBCurrent, fillingCCurrent, volumeC); break; case POUR_CA: finalizePour(fillingCCurrent, fillingACurrent, volumeA); break; case POUR_CB: finalizePour(fillingCCurrent, fillingBCurrent, volumeB); break; } } #pragma clang diagnostic pop bool checkPourSequences(unsigned int numberPours, unsigned int fillingFinal) { for(int i = 0; i < pow(NUMBER_OF_POURS_VARIANTS, numberPours); i++) { string poursSequenceCurrent = itoaCustom(i, NUMBER_OF_POURS_VARIANTS); string sequencePours; for(char digitIndex: poursSequenceCurrent) { sequencePours += digitIndex; } fillingACurrent = fillingAInitial; fillingBCurrent = fillingBInitial; fillingCCurrent = fillingCInitial; for(char typePour: sequencePours) { pour(typePour); } if(isPouringComplete(fillingFinal)) { return true; } } return false; } int getMinimalPours(unsigned int fillingFinal) { if(fillingFinal == fillingAInitial || fillingFinal == fillingBInitial || fillingFinal == fillingCInitial) { return 0; } if(fillingFinal > fillingAInitial + fillingBInitial + fillingCInitial) { return -1; } if(volumeA >= fillingFinal && (fillingFinal - fillingAInitial == fillingBInitial || fillingFinal - fillingAInitial == fillingCInitial )) { return 1; } if(volumeB >= fillingFinal && (fillingFinal - fillingBInitial == fillingAInitial || fillingFinal - fillingBInitial == fillingCInitial )) { return 1; } if(volumeC >= fillingFinal && (fillingFinal - fillingCInitial == fillingAInitial || fillingFinal - fillingCInitial == fillingBInitial )) { return 1; } if(fillingAInitial > fillingFinal && (fillingBInitial - volumeB == fillingFinal - fillingAInitial || fillingCInitial - volumeC == fillingFinal - fillingAInitial )) { return 1; } if(fillingBInitial > fillingFinal && (fillingAInitial - volumeA == fillingFinal - fillingBInitial || fillingCInitial - volumeC == fillingFinal - fillingBInitial )) { return 1; } if(fillingCInitial > fillingFinal && (fillingAInitial - volumeA == fillingFinal - fillingCInitial || fillingBInitial - volumeB == fillingFinal - fillingCInitial )) { return 1; } if(min(min(fillingAInitial + fillingBInitial, volumeA) + fillingCInitial, volumeC) == fillingFinal || min(min(fillingBInitial + fillingCInitial, volumeB) + fillingAInitial, volumeA) == fillingFinal || min(min(fillingCInitial + fillingAInitial, volumeC) + fillingBInitial, volumeB) == fillingFinal) { return 2; } int numberPours = 2; while(numberPours <= MAX_POURS) { if(checkPourSequences(numberPours, fillingFinal)) { break; } numberPours++; } return numberPours <= MAX_POURS ? numberPours : -1; } int main() { string result; scanf("%i %i %i\n%i %i %i", &volumeA, &volumeB, &volumeC, &fillingAInitial, &fillingBInitial, &fillingCInitial); for(int i = 0; i <= volumeC; i++) { result += (result.empty() ? "" : " ") + to_string( getMinimalPours(i) ); } cout << result; 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 174 175 176 177 178 179 180 181 182 183 | #include <iostream> #include <cmath> using namespace std; unsigned int volumeA, volumeB, volumeC; unsigned int fillingAInitial, fillingBInitial, fillingCInitial; unsigned int fillingACurrent, fillingBCurrent, fillingCCurrent; const char POUR_AB = '0'; const char POUR_AC = '1'; const char POUR_BA = '2'; const char POUR_BC = '3'; const char POUR_CA = '4'; const char POUR_CB = '5'; const string POURS_ALL_POSSIBILITIES = "012345"; const unsigned int NUMBER_OF_POURS_VARIANTS = 6; const unsigned int MAX_POURS = 5; string itoaCustom(int value, int base) { static char buffer[MAX_POURS] = {'0'}; int i = MAX_POURS - 1; for(; value && i; --i, value /= base) { buffer[i] = POURS_ALL_POSSIBILITIES[value % base]; } return buffer; } bool isPouringComplete(unsigned int fillingFinal) { return fillingFinal == fillingACurrent || fillingFinal == fillingBCurrent || fillingFinal == fillingCCurrent; } void finalizePour(unsigned int &fillingFrom, unsigned int &fillingTo, unsigned int volumeTo) { if(volumeTo - fillingTo > fillingFrom) { fillingTo += fillingFrom; fillingFrom = 0; } else { fillingFrom -= (volumeTo - fillingTo); fillingTo = volumeTo; } } #pragma clang diagnostic push #pragma ide diagnostic ignored "hicpp-multiway-paths-covered" void pour(char typePour) { switch(typePour) { case POUR_AB: finalizePour(fillingACurrent, fillingBCurrent, volumeB); break; case POUR_AC: finalizePour(fillingACurrent, fillingCCurrent, volumeC); break; case POUR_BA: finalizePour(fillingBCurrent, fillingACurrent, volumeA); break; case POUR_BC: finalizePour(fillingBCurrent, fillingCCurrent, volumeC); break; case POUR_CA: finalizePour(fillingCCurrent, fillingACurrent, volumeA); break; case POUR_CB: finalizePour(fillingCCurrent, fillingBCurrent, volumeB); break; } } #pragma clang diagnostic pop bool checkPourSequences(unsigned int numberPours, unsigned int fillingFinal) { for(int i = 0; i < pow(NUMBER_OF_POURS_VARIANTS, numberPours); i++) { string poursSequenceCurrent = itoaCustom(i, NUMBER_OF_POURS_VARIANTS); string sequencePours; for(char digitIndex: poursSequenceCurrent) { sequencePours += digitIndex; } fillingACurrent = fillingAInitial; fillingBCurrent = fillingBInitial; fillingCCurrent = fillingCInitial; for(char typePour: sequencePours) { pour(typePour); } if(isPouringComplete(fillingFinal)) { return true; } } return false; } int getMinimalPours(unsigned int fillingFinal) { if(fillingFinal == fillingAInitial || fillingFinal == fillingBInitial || fillingFinal == fillingCInitial) { return 0; } if(fillingFinal > fillingAInitial + fillingBInitial + fillingCInitial) { return -1; } if(volumeA >= fillingFinal && (fillingFinal - fillingAInitial == fillingBInitial || fillingFinal - fillingAInitial == fillingCInitial )) { return 1; } if(volumeB >= fillingFinal && (fillingFinal - fillingBInitial == fillingAInitial || fillingFinal - fillingBInitial == fillingCInitial )) { return 1; } if(volumeC >= fillingFinal && (fillingFinal - fillingCInitial == fillingAInitial || fillingFinal - fillingCInitial == fillingBInitial )) { return 1; } if(fillingAInitial > fillingFinal && (fillingBInitial - volumeB == fillingFinal - fillingAInitial || fillingCInitial - volumeC == fillingFinal - fillingAInitial )) { return 1; } if(fillingBInitial > fillingFinal && (fillingAInitial - volumeA == fillingFinal - fillingBInitial || fillingCInitial - volumeC == fillingFinal - fillingBInitial )) { return 1; } if(fillingCInitial > fillingFinal && (fillingAInitial - volumeA == fillingFinal - fillingCInitial || fillingBInitial - volumeB == fillingFinal - fillingCInitial )) { return 1; } if(min(min(fillingAInitial + fillingBInitial, volumeA) + fillingCInitial, volumeC) == fillingFinal || min(min(fillingBInitial + fillingCInitial, volumeB) + fillingAInitial, volumeA) == fillingFinal || min(min(fillingCInitial + fillingAInitial, volumeC) + fillingBInitial, volumeB) == fillingFinal) { return 2; } int numberPours = 2; while(numberPours <= MAX_POURS) { if(checkPourSequences(numberPours, fillingFinal)) { break; } numberPours++; } return numberPours <= MAX_POURS ? numberPours : -1; } int main() { string result; scanf("%i %i %i\n%i %i %i", &volumeA, &volumeB, &volumeC, &fillingAInitial, &fillingBInitial, &fillingCInitial); for(int i = 0; i <= volumeC; i++) { result += (result.empty() ? "" : " ") + to_string( getMinimalPours(i) ); } cout << result; return 0; } |