extern "C" { #include "cielib.h" } inline void initEntekPos(const int d, const int r, int* entek) { for (int i = 0; i < d; ++i) { entek[i] = r / 2; } } inline bool findFactors(int& k, const int d, const int r, int* entek, int* factors) { bool result = false; for (int i = 0; i < d; ++i) { factors[i] = 0; } for (int i = 0; i < d; ++i) { if (entek[i] == 0) { entek[i] += 1; int closer = czyCieplo(entek); k--; if (closer == 1) { factors[i] = 1; result = true; } else { entek[i] -= 1; closer = czyCieplo(entek); --k; if (closer == 1) { return false; // it is done! } else { entek[i] += 1; } } } else if (entek[i] == r) { entek[i] -= 1; int closer = czyCieplo(entek); k--; if (closer == 1) { result = true; factors[i] = -1; } else { entek[i] += 1; closer = czyCieplo(entek); --k; if (closer == 1) { return false; // it is done! } else { entek[i] -= 1; } } } else { entek[i] += 1; int closer = czyCieplo(entek); k--; if (closer == 0) { entek[i] -= 2; closer = czyCieplo(entek); k--; if (closer == 1) { entek[i] += 1; factors[i] = -1; result = true; } else { entek[i] += 1; closer = czyCieplo(entek); k--; if (closer == 1) { factors[i] = 1; result = true; } } } else { entek[i] -= 1; factors[i] = 1; result = true; break; } } } return result; } inline bool moveEntek(int& k, const int d, const int r, int* entek, int* factors, int& maxJumpSize) { bool notAllSet = false; for (int i = 0; i < d; ++i) { if (factors[i] > 0) { if (r - entek[i] < maxJumpSize) { maxJumpSize = r - entek[i]; } } else if (factors[i] < 0) { if (entek[i] < maxJumpSize) { maxJumpSize = entek[i]; } } else { notAllSet = true; } } int jump = maxJumpSize; bool firstSuccess = true; while (jump > 0) { for (int i = 0; i < d; ++i) { entek[i] += factors[i] * jump; } int closer = czyCieplo(entek); k--; if (closer) { if (firstSuccess) { maxJumpSize = 2 * jump; firstSuccess = false; break; } } else { for (int i = 0; i < d; ++i) { entek[i] -= factors[i] * jump; } closer = czyCieplo(entek); k--; if (closer == 0) { break; } } jump /= 2; } if (jump == 0 && !notAllSet) { return false; } return true; } int main() { int k, d, r; k = podajK(); d = podajD(); r = podajR(); int entek[500]; int factors[500]; initEntekPos(d, r, entek); czyCieplo(entek); k--; int maxJumpSize = r; while (true) { if (!findFactors(k, d, r, entek, factors)) { break; } if (!moveEntek(k, d, r, entek, factors, maxJumpSize)) { break; } } znalazlem(entek); 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 | extern "C" { #include "cielib.h" } inline void initEntekPos(const int d, const int r, int* entek) { for (int i = 0; i < d; ++i) { entek[i] = r / 2; } } inline bool findFactors(int& k, const int d, const int r, int* entek, int* factors) { bool result = false; for (int i = 0; i < d; ++i) { factors[i] = 0; } for (int i = 0; i < d; ++i) { if (entek[i] == 0) { entek[i] += 1; int closer = czyCieplo(entek); k--; if (closer == 1) { factors[i] = 1; result = true; } else { entek[i] -= 1; closer = czyCieplo(entek); --k; if (closer == 1) { return false; // it is done! } else { entek[i] += 1; } } } else if (entek[i] == r) { entek[i] -= 1; int closer = czyCieplo(entek); k--; if (closer == 1) { result = true; factors[i] = -1; } else { entek[i] += 1; closer = czyCieplo(entek); --k; if (closer == 1) { return false; // it is done! } else { entek[i] -= 1; } } } else { entek[i] += 1; int closer = czyCieplo(entek); k--; if (closer == 0) { entek[i] -= 2; closer = czyCieplo(entek); k--; if (closer == 1) { entek[i] += 1; factors[i] = -1; result = true; } else { entek[i] += 1; closer = czyCieplo(entek); k--; if (closer == 1) { factors[i] = 1; result = true; } } } else { entek[i] -= 1; factors[i] = 1; result = true; break; } } } return result; } inline bool moveEntek(int& k, const int d, const int r, int* entek, int* factors, int& maxJumpSize) { bool notAllSet = false; for (int i = 0; i < d; ++i) { if (factors[i] > 0) { if (r - entek[i] < maxJumpSize) { maxJumpSize = r - entek[i]; } } else if (factors[i] < 0) { if (entek[i] < maxJumpSize) { maxJumpSize = entek[i]; } } else { notAllSet = true; } } int jump = maxJumpSize; bool firstSuccess = true; while (jump > 0) { for (int i = 0; i < d; ++i) { entek[i] += factors[i] * jump; } int closer = czyCieplo(entek); k--; if (closer) { if (firstSuccess) { maxJumpSize = 2 * jump; firstSuccess = false; break; } } else { for (int i = 0; i < d; ++i) { entek[i] -= factors[i] * jump; } closer = czyCieplo(entek); k--; if (closer == 0) { break; } } jump /= 2; } if (jump == 0 && !notAllSet) { return false; } return true; } int main() { int k, d, r; k = podajK(); d = podajD(); r = podajR(); int entek[500]; int factors[500]; initEntekPos(d, r, entek); czyCieplo(entek); k--; int maxJumpSize = r; while (true) { if (!findFactors(k, d, r, entek, factors)) { break; } if (!moveEntek(k, d, r, entek, factors, maxJumpSize)) { break; } } znalazlem(entek); return 0; } |