#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace std; #define PB push_back #define MP make_pair #define LL long long #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define R(i,n) REP(i,n) #define VI vector<int> #define PII pair<int,int> #define LD long double #define FI first #define SE second #define st FI #define nd SE #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define unordered_map __fast_unordered_map template<class Key, class Value, class Hash = std::hash<Key>> using unordered_map = __gnu_pbds::gp_hash_table<Key, Value, Hash>; template<class C> void mini(C &a4, C b4) { a4 = min(a4, b4); } template<class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); } template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++; cerr<<'='<<h<<','; _dbg(sdbg+1, a...); } template<class T> ostream &operator<<(ostream& os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; return os << "]"; } template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.st << "," << P.nd << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif const int kSpawnCost = 100; const int kInitGold = 200; const int kGoldPickup = 10; struct Testcase { int N; vector<vector<int>> data; vector<int> farmer_r, farmer_c, farmer_gold; vector<int> tank_r, tank_c; vector<vector<bool>> is_taken; int cur_gold; bool HasGoldOnBoard() const { for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) if (data[i][j] > 0) return true; return false; } bool HasGoldToDeliver() const { for (int g : farmer_gold) if (g > 0) return true; return HasGoldOnBoard(); } bool MoveSingleUnit(int src_r, int src_c, int dest_r, int dest_c) { const int dr = dest_r - src_r; const int dc = dest_c - src_c; assert(abs(dr) + abs(dc) <= 1); if (dr == 0 && dc == 0) { return false; } if (is_taken[dest_r][dest_c]) { return false; } //if (data[dest_r][dest_c] < 0) { return false; } is_taken[dest_r][dest_c] = true; is_taken[src_r][src_c] = false; cout << "M " << src_r << " " << src_c << " " << dest_r << " " << dest_c << "\n"; return true; } bool MoveSingleFarmer(int idx, int dest_r, int dest_c) { const int src_r = farmer_r[idx]; const int src_c = farmer_c[idx]; if (data[dest_r][dest_c] < 0) return false; if (!MoveSingleUnit(src_r, src_c, dest_r, dest_c)) return false; farmer_r[idx] = dest_r; farmer_c[idx] = dest_c; return true; } void DoMoveSingleFarmer(int idx, int dest_r, int dest_c) { const bool result = MoveSingleFarmer(idx, dest_r, dest_c); (void)result; assert(result); } bool MoveSingleTank(int idx, int dest_r, int dest_c) { const int src_r = tank_r[idx]; const int src_c = tank_c[idx]; if (!MoveSingleUnit(src_r, src_c, dest_r, dest_c)) return false; tank_r[idx] = dest_r; tank_c[idx] = dest_c; return true; } void DoMoveSingleTank(int idx, int dest_r, int dest_c) { const bool result = MoveSingleTank(idx, dest_r, dest_c); (void)result; assert(result); } void SpawnFarmer() { assert(!is_taken[0][0]); farmer_r.PB(0); farmer_c.PB(0); farmer_gold.PB(0); is_taken[0][0] = true; cur_gold -= kSpawnCost; cout << "R FARMER\n"; } void SpawnTank() { assert(!is_taken[0][0]); tank_r.PB(0); tank_c.PB(0); is_taken[0][0] = true; cur_gold -= kSpawnCost; cout << "R TANK\n"; } void FarmersEndTurn() { for (int i = 0; i < SZ(farmer_r); ++i) { if (farmer_r[i] == 0 && farmer_c[i] == 0) { // Farmer is in base. cur_gold += farmer_gold[i]; farmer_gold[i] = 0; } else { // Farmer picks up gold from the cell. int &num_nuggets = data[farmer_r[i]][farmer_c[i]]; if (num_nuggets > 0) { const int num_picked = min(kGoldPickup, num_nuggets); num_nuggets -= num_picked; farmer_gold[i] += num_picked; } } } } void TanksEndTurn() { for (int i = 0; i < SZ(tank_r); ++i) { int &stones = data[tank_r[i]][tank_c[i]]; if (stones < 0) { stones = min<int>(0, stones + kGoldPickup); } } } int num_turns_so_far; void EndTurn() { FarmersEndTurn(); TanksEndTurn(); ++num_turns_so_far; cout << "=\n"; } void NoStonesStrategy() { // Check if the strategy is viable. assert(N == 20); const int phase1_needed_gold = (N - 2) * kSpawnCost; vector<int> row_size(N); for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) row_size[i] += data[i][j]; int fst = -1, snd = -1; for (int sec = 1; sec < N && snd == -1; ++sec) { for (int fir = 0; fir < sec; ++fir) { if (row_size[fir] + row_size[sec] >= phase1_needed_gold) { fst = fir; snd = sec; break; } } } assert(fst != -1); // Spawn two farmers in the first column. SpawnFarmer(); DoMoveSingleFarmer(0, 0, 1); SpawnFarmer(); DoMoveSingleFarmer(1, 1, 0); EndTurn(); DoMoveSingleFarmer(0, 0, 0); EndTurn(); // Move them to the respective rows. while (farmer_r[1] != snd || farmer_r[0] != fst) { if (farmer_r[1] < snd) DoMoveSingleFarmer(1, farmer_r[1] + 1, 0); if (farmer_r[0] < fst) DoMoveSingleFarmer(0, farmer_r[0] + 1, 0); EndTurn(); } // Loot the rows, but only take as much gold as necessary. while (farmer_gold[0] + farmer_gold[1] + cur_gold < phase1_needed_gold) { for (int b : {0, 1}) { if (data[farmer_r[b]][farmer_c[b]] > 0 || farmer_c[b] == N - 1) { continue; } DoMoveSingleFarmer(b, farmer_r[b], farmer_c[b] + 1); } EndTurn(); } // Go back to base and store gold. while (farmer_c[0] || farmer_c[1]) { for (int b : {0, 1}) { if (farmer_c[b]) { DoMoveSingleFarmer(b, farmer_r[b], farmer_c[b] - 1); } } EndTurn(); } while (farmer_r[0] != 0 || farmer_r[1] != 1) { if (farmer_r[0]) DoMoveSingleFarmer(0, farmer_r[0] - 1, 0); if (farmer_r[1] > 1) DoMoveSingleFarmer(1, farmer_r[1] - 1, 0); EndTurn(); } DoMoveSingleFarmer(0, 0, 1); DoMoveSingleFarmer(1, 0, 0); EndTurn(); assert(cur_gold >= phase1_needed_gold); //Now, spawn the farmers to have N of them in total and populate the columns. for (int spawn_step = 2; spawn_step < N; ++spawn_step) { for (int i = 0; i < spawn_step; ++i) DoMoveSingleFarmer(i, 0, spawn_step - i); SpawnFarmer(); EndTurn(); } // Loot the rows. while (HasGoldOnBoard()) { for (int b = 0; b < N; ++b) { if (data[farmer_r[b]][farmer_c[b]] > 0 || farmer_r[b] == N - 1) { continue; } DoMoveSingleFarmer(b, farmer_r[b] + 1, farmer_c[b]); } EndTurn(); } // Go back to base and store gold. for (int i = N - 1; i > 0; --i) { for (int j = 0; j < N; ++j) DoMoveSingleFarmer(j, i - 1, farmer_c[j]); EndTurn(); } for (int i = 0; i < N - 1; ++i) { for (int who = N - 1; who >= 0; --who) { if (farmer_c[who] == 0) DoMoveSingleFarmer(who, farmer_r[who] + 1, farmer_c[who]); else DoMoveSingleFarmer(who, farmer_r[who], farmer_c[who] - 1); } EndTurn(); } const int still_has_gold = HasGoldOnBoard(); assert(!still_has_gold); cout << "===\n"; } void StonesStrategy() { // Spawn a tank and a farmer. SpawnTank(); DoMoveSingleTank(0, 0, 1); SpawnFarmer(); EndTurn(); // Do a zig-zag in the first rows of the board, // collecting money needed for spawning more units. // However, to make things easier, *always* clean up the first row. const int phase1_needed_gold = (N - 2) * kSpawnCost; vector<PII> zigzag_pattern; for (int r = 0; r < N; ++r) { if (r % 2 == 1) { for (int c = N - 1; c >= 0; --c) zigzag_pattern.emplace_back(r, c); } else { for (int c = 0; c < N; ++c) zigzag_pattern.emplace_back(r, c); } } int tank_position = 1, farmer_position = 0; while (farmer_gold[0] + cur_gold < phase1_needed_gold || tank_position <= N) { // Move tank? if (data[tank_r[0]][tank_c[0]] >= 0) { ++tank_position; DoMoveSingleTank(0, zigzag_pattern[tank_position].st, zigzag_pattern[tank_position].nd); } // Move farmer? if (farmer_position < tank_position - 1 && data[farmer_r[0]][farmer_c[0]] == 0) { ++farmer_position; DoMoveSingleFarmer(0, zigzag_pattern[farmer_position].st, zigzag_pattern[farmer_position].nd); } EndTurn(); } debug(farmer_position, tank_position); debug(num_turns_so_far); // Get back to base. // TODO: can do it way quicker than that. while (farmer_position != 0 || tank_position != 1) { if (farmer_position) { --farmer_position; DoMoveSingleFarmer(0, zigzag_pattern[farmer_position].st, zigzag_pattern[farmer_position].nd); } if (tank_position > 1) { --tank_position; DoMoveSingleTank(0, zigzag_pattern[tank_position].st, zigzag_pattern[tank_position].nd); } EndTurn(); } debug(num_turns_so_far); debug(cur_gold); // Populate the first row by tanks and farmers. for (int step = 2; step < N; ++step) { for (int i = 0; i < step; ++i) { if (i % 2 == 0) { DoMoveSingleTank(i / 2, 0, tank_c[i / 2] + 1); } else { DoMoveSingleFarmer(i / 2, 0, farmer_c[i / 2] + 1); } } if (step % 2 == 0) { SpawnTank(); } else { SpawnFarmer(); } EndTurn(); } // Divide the board into column stripes of width 2. Having one tank // and one farmer in each stripe, loot all the gold *and* stones from there. assert(N % 2 == 0); vector<vector<PII>> tank_path(N / 2), farmer_path(N / 2); for (int who = 0; who < N / 2; ++who) { const int stripe = N / 2 - who - 1; for (int i = 0; i < N; ++i) tank_path[who].emplace_back(i, stripe * 2 + 1); for (int i = N - 1; i >= 0; --i) tank_path[who].emplace_back(i, stripe * 2); tank_path[who].emplace_back(0, stripe * 2 + 1); farmer_path[who].emplace_back(0, stripe * 2); copy(ALL(tank_path[who]), back_inserter(farmer_path[who])); tank_path[who].emplace_back(1, stripe * 2 + 1); } debug(tank_path[1], farmer_path[1]); const int path_len = SZ(tank_path[0]); assert(SZ(farmer_path[0]) == path_len); vector<int> tank_path_idx(N / 2), farmer_path_idx(N / 2); while (count(ALL(farmer_path_idx), path_len - 1) != N / 2) { for (int i = 0; i < N / 2; ++i) { // Try to move the tank? if (tank_path_idx[i] < path_len - 1 && data[tank_r[i]][tank_c[i]] >= 0) { ++tank_path_idx[i]; if (!MoveSingleTank(i, tank_path[i][tank_path_idx[i]].st, tank_path[i][tank_path_idx[i]].nd)) { --tank_path_idx[i]; } } // Try to move the farmer? if (farmer_path_idx[i] < tank_path_idx[i] && data[farmer_r[i]][farmer_c[i]] == 0) { ++farmer_path_idx[i]; DoMoveSingleFarmer(i, farmer_path[i][farmer_path_idx[i]].st, farmer_path[i][farmer_path_idx[i]].nd); } } EndTurn(); } // Now the farmers are in the first row, and the tanks are in the second row // (and not in the first column). Use this to get the farmers back to base. while (HasGoldToDeliver()) { for (int i = N / 2 - 1; i >= 0; --i) { if (farmer_c[i] == 0 && farmer_r[i] < i) { DoMoveSingleFarmer(i, farmer_r[i] + 1, 0); } else if (farmer_c[i] > 0) { assert(farmer_r[i] == 0); DoMoveSingleFarmer(i, 0, farmer_c[i] - 1); } } EndTurn(); } cout << "===\n"; } void Run() { cin >> N; data.resize(N, vector<int>(N)); bool has_stones = false; for (auto &row : data) for (int &elem : row) { cin >> elem; has_stones |= (elem < 0); } is_taken.resize(N, vector<bool>(N)); cur_gold = kInitGold; num_turns_so_far = 0; if (!has_stones) { NoStonesStrategy(); } else { StonesStrategy(); } } }; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(11); cerr << fixed << setprecision(6); int T, k; cin >> T >> k; for (int i = 0; i < T; ++i) Testcase().Run(); }
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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace std; #define PB push_back #define MP make_pair #define LL long long #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define R(i,n) REP(i,n) #define VI vector<int> #define PII pair<int,int> #define LD long double #define FI first #define SE second #define st FI #define nd SE #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define unordered_map __fast_unordered_map template<class Key, class Value, class Hash = std::hash<Key>> using unordered_map = __gnu_pbds::gp_hash_table<Key, Value, Hash>; template<class C> void mini(C &a4, C b4) { a4 = min(a4, b4); } template<class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); } template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++; cerr<<'='<<h<<','; _dbg(sdbg+1, a...); } template<class T> ostream &operator<<(ostream& os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; return os << "]"; } template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.st << "," << P.nd << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif const int kSpawnCost = 100; const int kInitGold = 200; const int kGoldPickup = 10; struct Testcase { int N; vector<vector<int>> data; vector<int> farmer_r, farmer_c, farmer_gold; vector<int> tank_r, tank_c; vector<vector<bool>> is_taken; int cur_gold; bool HasGoldOnBoard() const { for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) if (data[i][j] > 0) return true; return false; } bool HasGoldToDeliver() const { for (int g : farmer_gold) if (g > 0) return true; return HasGoldOnBoard(); } bool MoveSingleUnit(int src_r, int src_c, int dest_r, int dest_c) { const int dr = dest_r - src_r; const int dc = dest_c - src_c; assert(abs(dr) + abs(dc) <= 1); if (dr == 0 && dc == 0) { return false; } if (is_taken[dest_r][dest_c]) { return false; } //if (data[dest_r][dest_c] < 0) { return false; } is_taken[dest_r][dest_c] = true; is_taken[src_r][src_c] = false; cout << "M " << src_r << " " << src_c << " " << dest_r << " " << dest_c << "\n"; return true; } bool MoveSingleFarmer(int idx, int dest_r, int dest_c) { const int src_r = farmer_r[idx]; const int src_c = farmer_c[idx]; if (data[dest_r][dest_c] < 0) return false; if (!MoveSingleUnit(src_r, src_c, dest_r, dest_c)) return false; farmer_r[idx] = dest_r; farmer_c[idx] = dest_c; return true; } void DoMoveSingleFarmer(int idx, int dest_r, int dest_c) { const bool result = MoveSingleFarmer(idx, dest_r, dest_c); (void)result; assert(result); } bool MoveSingleTank(int idx, int dest_r, int dest_c) { const int src_r = tank_r[idx]; const int src_c = tank_c[idx]; if (!MoveSingleUnit(src_r, src_c, dest_r, dest_c)) return false; tank_r[idx] = dest_r; tank_c[idx] = dest_c; return true; } void DoMoveSingleTank(int idx, int dest_r, int dest_c) { const bool result = MoveSingleTank(idx, dest_r, dest_c); (void)result; assert(result); } void SpawnFarmer() { assert(!is_taken[0][0]); farmer_r.PB(0); farmer_c.PB(0); farmer_gold.PB(0); is_taken[0][0] = true; cur_gold -= kSpawnCost; cout << "R FARMER\n"; } void SpawnTank() { assert(!is_taken[0][0]); tank_r.PB(0); tank_c.PB(0); is_taken[0][0] = true; cur_gold -= kSpawnCost; cout << "R TANK\n"; } void FarmersEndTurn() { for (int i = 0; i < SZ(farmer_r); ++i) { if (farmer_r[i] == 0 && farmer_c[i] == 0) { // Farmer is in base. cur_gold += farmer_gold[i]; farmer_gold[i] = 0; } else { // Farmer picks up gold from the cell. int &num_nuggets = data[farmer_r[i]][farmer_c[i]]; if (num_nuggets > 0) { const int num_picked = min(kGoldPickup, num_nuggets); num_nuggets -= num_picked; farmer_gold[i] += num_picked; } } } } void TanksEndTurn() { for (int i = 0; i < SZ(tank_r); ++i) { int &stones = data[tank_r[i]][tank_c[i]]; if (stones < 0) { stones = min<int>(0, stones + kGoldPickup); } } } int num_turns_so_far; void EndTurn() { FarmersEndTurn(); TanksEndTurn(); ++num_turns_so_far; cout << "=\n"; } void NoStonesStrategy() { // Check if the strategy is viable. assert(N == 20); const int phase1_needed_gold = (N - 2) * kSpawnCost; vector<int> row_size(N); for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) row_size[i] += data[i][j]; int fst = -1, snd = -1; for (int sec = 1; sec < N && snd == -1; ++sec) { for (int fir = 0; fir < sec; ++fir) { if (row_size[fir] + row_size[sec] >= phase1_needed_gold) { fst = fir; snd = sec; break; } } } assert(fst != -1); // Spawn two farmers in the first column. SpawnFarmer(); DoMoveSingleFarmer(0, 0, 1); SpawnFarmer(); DoMoveSingleFarmer(1, 1, 0); EndTurn(); DoMoveSingleFarmer(0, 0, 0); EndTurn(); // Move them to the respective rows. while (farmer_r[1] != snd || farmer_r[0] != fst) { if (farmer_r[1] < snd) DoMoveSingleFarmer(1, farmer_r[1] + 1, 0); if (farmer_r[0] < fst) DoMoveSingleFarmer(0, farmer_r[0] + 1, 0); EndTurn(); } // Loot the rows, but only take as much gold as necessary. while (farmer_gold[0] + farmer_gold[1] + cur_gold < phase1_needed_gold) { for (int b : {0, 1}) { if (data[farmer_r[b]][farmer_c[b]] > 0 || farmer_c[b] == N - 1) { continue; } DoMoveSingleFarmer(b, farmer_r[b], farmer_c[b] + 1); } EndTurn(); } // Go back to base and store gold. while (farmer_c[0] || farmer_c[1]) { for (int b : {0, 1}) { if (farmer_c[b]) { DoMoveSingleFarmer(b, farmer_r[b], farmer_c[b] - 1); } } EndTurn(); } while (farmer_r[0] != 0 || farmer_r[1] != 1) { if (farmer_r[0]) DoMoveSingleFarmer(0, farmer_r[0] - 1, 0); if (farmer_r[1] > 1) DoMoveSingleFarmer(1, farmer_r[1] - 1, 0); EndTurn(); } DoMoveSingleFarmer(0, 0, 1); DoMoveSingleFarmer(1, 0, 0); EndTurn(); assert(cur_gold >= phase1_needed_gold); //Now, spawn the farmers to have N of them in total and populate the columns. for (int spawn_step = 2; spawn_step < N; ++spawn_step) { for (int i = 0; i < spawn_step; ++i) DoMoveSingleFarmer(i, 0, spawn_step - i); SpawnFarmer(); EndTurn(); } // Loot the rows. while (HasGoldOnBoard()) { for (int b = 0; b < N; ++b) { if (data[farmer_r[b]][farmer_c[b]] > 0 || farmer_r[b] == N - 1) { continue; } DoMoveSingleFarmer(b, farmer_r[b] + 1, farmer_c[b]); } EndTurn(); } // Go back to base and store gold. for (int i = N - 1; i > 0; --i) { for (int j = 0; j < N; ++j) DoMoveSingleFarmer(j, i - 1, farmer_c[j]); EndTurn(); } for (int i = 0; i < N - 1; ++i) { for (int who = N - 1; who >= 0; --who) { if (farmer_c[who] == 0) DoMoveSingleFarmer(who, farmer_r[who] + 1, farmer_c[who]); else DoMoveSingleFarmer(who, farmer_r[who], farmer_c[who] - 1); } EndTurn(); } const int still_has_gold = HasGoldOnBoard(); assert(!still_has_gold); cout << "===\n"; } void StonesStrategy() { // Spawn a tank and a farmer. SpawnTank(); DoMoveSingleTank(0, 0, 1); SpawnFarmer(); EndTurn(); // Do a zig-zag in the first rows of the board, // collecting money needed for spawning more units. // However, to make things easier, *always* clean up the first row. const int phase1_needed_gold = (N - 2) * kSpawnCost; vector<PII> zigzag_pattern; for (int r = 0; r < N; ++r) { if (r % 2 == 1) { for (int c = N - 1; c >= 0; --c) zigzag_pattern.emplace_back(r, c); } else { for (int c = 0; c < N; ++c) zigzag_pattern.emplace_back(r, c); } } int tank_position = 1, farmer_position = 0; while (farmer_gold[0] + cur_gold < phase1_needed_gold || tank_position <= N) { // Move tank? if (data[tank_r[0]][tank_c[0]] >= 0) { ++tank_position; DoMoveSingleTank(0, zigzag_pattern[tank_position].st, zigzag_pattern[tank_position].nd); } // Move farmer? if (farmer_position < tank_position - 1 && data[farmer_r[0]][farmer_c[0]] == 0) { ++farmer_position; DoMoveSingleFarmer(0, zigzag_pattern[farmer_position].st, zigzag_pattern[farmer_position].nd); } EndTurn(); } debug(farmer_position, tank_position); debug(num_turns_so_far); // Get back to base. // TODO: can do it way quicker than that. while (farmer_position != 0 || tank_position != 1) { if (farmer_position) { --farmer_position; DoMoveSingleFarmer(0, zigzag_pattern[farmer_position].st, zigzag_pattern[farmer_position].nd); } if (tank_position > 1) { --tank_position; DoMoveSingleTank(0, zigzag_pattern[tank_position].st, zigzag_pattern[tank_position].nd); } EndTurn(); } debug(num_turns_so_far); debug(cur_gold); // Populate the first row by tanks and farmers. for (int step = 2; step < N; ++step) { for (int i = 0; i < step; ++i) { if (i % 2 == 0) { DoMoveSingleTank(i / 2, 0, tank_c[i / 2] + 1); } else { DoMoveSingleFarmer(i / 2, 0, farmer_c[i / 2] + 1); } } if (step % 2 == 0) { SpawnTank(); } else { SpawnFarmer(); } EndTurn(); } // Divide the board into column stripes of width 2. Having one tank // and one farmer in each stripe, loot all the gold *and* stones from there. assert(N % 2 == 0); vector<vector<PII>> tank_path(N / 2), farmer_path(N / 2); for (int who = 0; who < N / 2; ++who) { const int stripe = N / 2 - who - 1; for (int i = 0; i < N; ++i) tank_path[who].emplace_back(i, stripe * 2 + 1); for (int i = N - 1; i >= 0; --i) tank_path[who].emplace_back(i, stripe * 2); tank_path[who].emplace_back(0, stripe * 2 + 1); farmer_path[who].emplace_back(0, stripe * 2); copy(ALL(tank_path[who]), back_inserter(farmer_path[who])); tank_path[who].emplace_back(1, stripe * 2 + 1); } debug(tank_path[1], farmer_path[1]); const int path_len = SZ(tank_path[0]); assert(SZ(farmer_path[0]) == path_len); vector<int> tank_path_idx(N / 2), farmer_path_idx(N / 2); while (count(ALL(farmer_path_idx), path_len - 1) != N / 2) { for (int i = 0; i < N / 2; ++i) { // Try to move the tank? if (tank_path_idx[i] < path_len - 1 && data[tank_r[i]][tank_c[i]] >= 0) { ++tank_path_idx[i]; if (!MoveSingleTank(i, tank_path[i][tank_path_idx[i]].st, tank_path[i][tank_path_idx[i]].nd)) { --tank_path_idx[i]; } } // Try to move the farmer? if (farmer_path_idx[i] < tank_path_idx[i] && data[farmer_r[i]][farmer_c[i]] == 0) { ++farmer_path_idx[i]; DoMoveSingleFarmer(i, farmer_path[i][farmer_path_idx[i]].st, farmer_path[i][farmer_path_idx[i]].nd); } } EndTurn(); } // Now the farmers are in the first row, and the tanks are in the second row // (and not in the first column). Use this to get the farmers back to base. while (HasGoldToDeliver()) { for (int i = N / 2 - 1; i >= 0; --i) { if (farmer_c[i] == 0 && farmer_r[i] < i) { DoMoveSingleFarmer(i, farmer_r[i] + 1, 0); } else if (farmer_c[i] > 0) { assert(farmer_r[i] == 0); DoMoveSingleFarmer(i, 0, farmer_c[i] - 1); } } EndTurn(); } cout << "===\n"; } void Run() { cin >> N; data.resize(N, vector<int>(N)); bool has_stones = false; for (auto &row : data) for (int &elem : row) { cin >> elem; has_stones |= (elem < 0); } is_taken.resize(N, vector<bool>(N)); cur_gold = kInitGold; num_turns_so_far = 0; if (!has_stones) { NoStonesStrategy(); } else { StonesStrategy(); } } }; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(11); cerr << fixed << setprecision(6); int T, k; cin >> T >> k; for (int i = 0; i < T; ++i) Testcase().Run(); } |