#include <vector> #include <cmath> #include <queue> #include <unordered_set> #include <iostream> #include "cielib.h" #define DEBUG(A) //std::cerr << A << std::endl; #define DEBUG2(A) //std::cerr << A; class finder_t { protected: int dims; int range; int shot; int max_shots; std::vector<int> from_0; std::vector<int> to_0; std::vector<int> from; std::vector<int> to; std::vector<int> tmp; public: finder_t(int dims, int range, int max_shots) : dims(dims), range(range), shot(-1), max_shots(max_shots), from_0(dims, 0), to_0(dims, range), from(dims, 0), to(dims, range), tmp(dims, 0) { } void find_her(); protected: int ask(std::vector<int> &guess) { ++shot; //DEBUG("Shot " << shot); return czyCieplo(&guess[0]); } void get_middle(std::vector<int> &a, int a_ind, std::vector<int> &b, int &b_ind, int off1, int off2) { b_ind = (a_ind + b_ind + off1) / 2 + off2; for(int d = 0; d < dims; ++d) { b[d] = (a[d] + b[d] + off1) / 2 + off2; } } void find_mid(std::vector<int> &from, std::vector<int> &to, int which); }; void finder_t::find_mid(std::vector<int> &from, std::vector<int> &to, int which) { // From is assumed to be prepared // Prepare to DEBUG(which); for(int d = 0; d < dims; ++d) { from[d] = (d == which) ? range : 0; to[d] = (from[d] == 0) ? range : 0; DEBUG(from[d] << " " << to[d]); } int from_ind = 0; int to_ind = range; int dist = std::abs(to_ind - from_ind); int dist_was_1 = 0; // Initialize search ask(from); while(dist > 0 && dist_was_1 < 2) { DEBUG("ask: " << from_ind << "->" << to_ind); bool better = ask(to); // Swapping from and to std::swap(from, to); std::swap(from_ind, to_ind); // Select middle - from if(better) { DEBUG(from_ind << " CLOSER than " << to_ind); if(from_ind > to_ind) { get_middle(from, from_ind, to, to_ind, 1, 0); } else { get_middle(from, from_ind, to, to_ind, 0, 0); } } // Select to - middle else { DEBUG(from_ind << " NOT closer than " << to_ind); DEBUG("ask: " << from_ind << "->" << to_ind); ask(to); std::swap(from, to); std::swap(from_ind, to_ind); if(from_ind > to_ind) { get_middle(from, from_ind, to, to_ind, 0, 0); } else { get_middle(from, from_ind, to, to_ind, 1, 0); } } dist = std::abs(to_ind - from_ind); if(dist == 1) { ++dist_was_1; } DEBUG(from_ind << " " << to_ind); } DEBUG(""); for(int d = 0; d < dims; ++d) { DEBUG(from[d] << " " << to[d]); } } void finder_t::find_her() { int d = 0; int found = 0; int counter = 0; ask(tmp); for(; found < dims && shot < max_shots; d = (d + 1) % 2) { DEBUG(d); if(std::abs(from[d] - to[d]) > 0) { // New shot int &new_val = (tmp[d] == from[d]) ? to[d] : from[d]; int &old_val = (tmp[d] == from[d]) ? from[d] : to[d]; DEBUG("old " << old_val << " -> " << new_val); tmp[d] = new_val; if(ask(tmp)) { counter = 0; DEBUG2("cahnge " << old_val); if(old_val < new_val) { old_val = (old_val + new_val) / 2 + 1; } else { old_val = (old_val + new_val) / 2; } DEBUG(" -> " << old_val); found += (std::abs(from[d] - to[d]) == 0); } else if(counter == dims) { counter = 0; old_val += (old_val > new_val) ? -1 : 1; found += (std::abs(from[d] - to[d]) == 0); } else { ++counter; } } for(auto v : from) { DEBUG2(v << " "); } DEBUG(""); for(auto v : tmp) { DEBUG2(v << " "); } DEBUG(""); for(auto v : to) { DEBUG2(v << " "); } DEBUG("\n************"); } znalazlem(&tmp[0]); /* find_mid(from_0, to_0, -1); DEBUG(-1 << ")" << from_0[0] << " " << to_0[0] << "\n**********"); for(int d = 0; d < dims; ++d) { find_mid(from_0, to_0, d); DEBUG(d << ")" << from_0[d] << " " << to_0[d] << "\n**********"); } /* int range; int to; // Initialize try_current(); // Until everything is found or we run out of guesses while(shot < max_shots && !to_do.empty()) { // Get the dimension with the biggest undefined range for(auto &d : to_do) { if(shot >= max_shots) { break; } range = get_range(d); for(auto i : current) { std::cerr << i << " "; } std::cerr << std::endl; for(auto i : next) { std::cerr << i << " "; } std::cerr << std::endl; // Found if(range == 0) { to_do.erase(d); break; } std::swap(current[d], next[d]); // If the try is closer - half the range to be scanned if(try_current()) { std::cerr << d << ": " << current[d] << " IS closer than " << next[d] << std::endl; if(next[d] < current[d]) { next[d] = (range + 1) / 2; } else { next[d] = (range + 1) / 2 - 1; } std::cerr << "Halfed: " << d << " " << current[d] << " " << next[d] << std::endl; } else { std::cerr << d << ": " << current[d] << " IS NOT closer than " << next[d] << std::endl; /*if(next[d] < current[d]) { next[d] = (range + 1) / 2; } else { next[d] = (range + 1) / 2 - 1; } std::cerr << d << ": " << current[d] << " is NOT closer than " << next[d] << std::endl; std::swap(current[d], next[d]); // If the other one is closer though if(try_current()) { std::cerr << d << ": " << current[d] << " IS closer than " << next[d] << std::endl; if(next[d] < current[d]) { next[d] = (range + 1) / 2; } else { next[d] = (range + 1) / 2 - 1; } std::cerr << "Shortened: " << d << " " << current[d] << " " << next[d] << std::endl; } else { std::cerr << current[d] << " in NOT closer than " << next[d] << std::endl; } } } } znalazlem(¤t[0]); */ } int main() { finder_t finder(podajD(), podajR(), podajK()); finder.find_her(); 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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | #include <vector> #include <cmath> #include <queue> #include <unordered_set> #include <iostream> #include "cielib.h" #define DEBUG(A) //std::cerr << A << std::endl; #define DEBUG2(A) //std::cerr << A; class finder_t { protected: int dims; int range; int shot; int max_shots; std::vector<int> from_0; std::vector<int> to_0; std::vector<int> from; std::vector<int> to; std::vector<int> tmp; public: finder_t(int dims, int range, int max_shots) : dims(dims), range(range), shot(-1), max_shots(max_shots), from_0(dims, 0), to_0(dims, range), from(dims, 0), to(dims, range), tmp(dims, 0) { } void find_her(); protected: int ask(std::vector<int> &guess) { ++shot; //DEBUG("Shot " << shot); return czyCieplo(&guess[0]); } void get_middle(std::vector<int> &a, int a_ind, std::vector<int> &b, int &b_ind, int off1, int off2) { b_ind = (a_ind + b_ind + off1) / 2 + off2; for(int d = 0; d < dims; ++d) { b[d] = (a[d] + b[d] + off1) / 2 + off2; } } void find_mid(std::vector<int> &from, std::vector<int> &to, int which); }; void finder_t::find_mid(std::vector<int> &from, std::vector<int> &to, int which) { // From is assumed to be prepared // Prepare to DEBUG(which); for(int d = 0; d < dims; ++d) { from[d] = (d == which) ? range : 0; to[d] = (from[d] == 0) ? range : 0; DEBUG(from[d] << " " << to[d]); } int from_ind = 0; int to_ind = range; int dist = std::abs(to_ind - from_ind); int dist_was_1 = 0; // Initialize search ask(from); while(dist > 0 && dist_was_1 < 2) { DEBUG("ask: " << from_ind << "->" << to_ind); bool better = ask(to); // Swapping from and to std::swap(from, to); std::swap(from_ind, to_ind); // Select middle - from if(better) { DEBUG(from_ind << " CLOSER than " << to_ind); if(from_ind > to_ind) { get_middle(from, from_ind, to, to_ind, 1, 0); } else { get_middle(from, from_ind, to, to_ind, 0, 0); } } // Select to - middle else { DEBUG(from_ind << " NOT closer than " << to_ind); DEBUG("ask: " << from_ind << "->" << to_ind); ask(to); std::swap(from, to); std::swap(from_ind, to_ind); if(from_ind > to_ind) { get_middle(from, from_ind, to, to_ind, 0, 0); } else { get_middle(from, from_ind, to, to_ind, 1, 0); } } dist = std::abs(to_ind - from_ind); if(dist == 1) { ++dist_was_1; } DEBUG(from_ind << " " << to_ind); } DEBUG(""); for(int d = 0; d < dims; ++d) { DEBUG(from[d] << " " << to[d]); } } void finder_t::find_her() { int d = 0; int found = 0; int counter = 0; ask(tmp); for(; found < dims && shot < max_shots; d = (d + 1) % 2) { DEBUG(d); if(std::abs(from[d] - to[d]) > 0) { // New shot int &new_val = (tmp[d] == from[d]) ? to[d] : from[d]; int &old_val = (tmp[d] == from[d]) ? from[d] : to[d]; DEBUG("old " << old_val << " -> " << new_val); tmp[d] = new_val; if(ask(tmp)) { counter = 0; DEBUG2("cahnge " << old_val); if(old_val < new_val) { old_val = (old_val + new_val) / 2 + 1; } else { old_val = (old_val + new_val) / 2; } DEBUG(" -> " << old_val); found += (std::abs(from[d] - to[d]) == 0); } else if(counter == dims) { counter = 0; old_val += (old_val > new_val) ? -1 : 1; found += (std::abs(from[d] - to[d]) == 0); } else { ++counter; } } for(auto v : from) { DEBUG2(v << " "); } DEBUG(""); for(auto v : tmp) { DEBUG2(v << " "); } DEBUG(""); for(auto v : to) { DEBUG2(v << " "); } DEBUG("\n************"); } znalazlem(&tmp[0]); /* find_mid(from_0, to_0, -1); DEBUG(-1 << ")" << from_0[0] << " " << to_0[0] << "\n**********"); for(int d = 0; d < dims; ++d) { find_mid(from_0, to_0, d); DEBUG(d << ")" << from_0[d] << " " << to_0[d] << "\n**********"); } /* int range; int to; // Initialize try_current(); // Until everything is found or we run out of guesses while(shot < max_shots && !to_do.empty()) { // Get the dimension with the biggest undefined range for(auto &d : to_do) { if(shot >= max_shots) { break; } range = get_range(d); for(auto i : current) { std::cerr << i << " "; } std::cerr << std::endl; for(auto i : next) { std::cerr << i << " "; } std::cerr << std::endl; // Found if(range == 0) { to_do.erase(d); break; } std::swap(current[d], next[d]); // If the try is closer - half the range to be scanned if(try_current()) { std::cerr << d << ": " << current[d] << " IS closer than " << next[d] << std::endl; if(next[d] < current[d]) { next[d] = (range + 1) / 2; } else { next[d] = (range + 1) / 2 - 1; } std::cerr << "Halfed: " << d << " " << current[d] << " " << next[d] << std::endl; } else { std::cerr << d << ": " << current[d] << " IS NOT closer than " << next[d] << std::endl; /*if(next[d] < current[d]) { next[d] = (range + 1) / 2; } else { next[d] = (range + 1) / 2 - 1; } std::cerr << d << ": " << current[d] << " is NOT closer than " << next[d] << std::endl; std::swap(current[d], next[d]); // If the other one is closer though if(try_current()) { std::cerr << d << ": " << current[d] << " IS closer than " << next[d] << std::endl; if(next[d] < current[d]) { next[d] = (range + 1) / 2; } else { next[d] = (range + 1) / 2 - 1; } std::cerr << "Shortened: " << d << " " << current[d] << " " << next[d] << std::endl; } else { std::cerr << current[d] << " in NOT closer than " << next[d] << std::endl; } } } } znalazlem(¤t[0]); */ } int main() { finder_t finder(podajD(), podajR(), podajK()); finder.find_her(); return 0; } |