#include "bits/stdc++.h" using namespace std; #define PB push_back #define LL long long #define FOR(i,a,b) for (int i = (a); i <= (b); i++) #define FORD(i,a,b) for (int i = (a); i >= (b); i--) #define REP(i,n) FOR(i,0,(int)(n)-1) #define st first #define nd second #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define VI vector<int> #define PII pair<int,int> #define LD long double #define X real() #define Y imag() 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<<"\n";} 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 string RED = "\033[41m"; const string GREEN = "\033[42;30m"; const string BLUE = "\033[44m"; const string CLEAR = "\033[0m"; typedef PII P; bool between(int m, int l, int r){ return m >= l && m <= r; } string get_string(int a){ string s = to_string(a); s = string(4 - SZ(s), ' ') + s; return s; } enum Farmer_State{ DEPOSITING, RETURNING, MINING }; struct Board; struct Farmer; struct Pawn{ P coords; int32_t & x, y; Board & b; int turn_active; int lane; Pawn(Board & _b); virtual ~Pawn(){}; virtual void end_turn() = 0; virtual bool can_move(PII d); virtual void move(PII d); virtual string get_string() = 0; bool in_base(); virtual bool operate(); virtual bool soft_operate(); virtual bool is_farmer(){ return false; } }; struct Farmer: public Pawn{ int gold; Farmer(Board & _b); bool can_move(PII d); string get_string(); void end_turn(); bool operate(); bool soft_operate(); bool is_farmer(){ return true; } }; struct Tank : public Pawn{ Tank(Board & _b); string get_string(); void end_turn(); bool operate(); bool soft_operate(); }; struct Board{ int n; vector<VI> board; vector<vector<int>> taken; vector<Tank*> tanks; vector<Farmer*> farmers; vector<Pawn*> pawns; vector<vector<Pawn*>> pawn_of_field; vector<vector<PII>> where_to; vector<vector<PII>> military_where_to; vector<vector<PII>> routes; vector<vector<PII>> military_routes; vector<map<PII, PII>> military_ramps; int route_idx; int military_route_idx; bool stones; int gold; int total_gold; int turn; Board(int _n): n(_n), board(n, VI(n)), taken(n, vector<int>(n)), pawn_of_field(n, vector<Pawn*>(n, 0)), where_to(n, vector<PII>(n)), military_where_to(n, vector<PII>(n)), stones(false), gold(200), total_gold(0), turn(0) { REP(i, n){ REP(j, n){ cin>>board[i][j]; if(board[i][j] < 0){ stones = true; } else{ total_gold += board[i][j]; } } } route_idx = -1; military_route_idx = -1; for(int y = 2; y < n; y += 2){ int x = 0; PII cur = {0,0}; vector<PII> cur_route = {cur}; while(cur.nd != x + 1){ cur.nd++; cur_route.PB(cur); } while(cur.st != y){ cur.st++; cur_route.PB(cur); } cur.nd--; cur_route.PB(cur); while(cur.st != 1){ cur.st--; cur_route.PB(cur); } while(cur.nd != 0){ cur.nd--; cur_route.PB(cur); } routes.PB(cur_route); } for(int x = 0; x < n; x += 2){ PII cur = {0,0}; vector<PII> cur_route = {cur}; if(x == n - 1){ x = n - 2; } while(cur.nd != x + 1){ cur.nd++; cur_route.PB(cur); } while(cur.st != n - 1){ cur.st++; cur_route.PB(cur); } cur.nd--; cur_route.PB(cur); while(cur.st != 1){ cur.st--; cur_route.PB(cur); } while(cur.nd != 0){ cur.nd--; cur_route.PB(cur); } routes.PB(cur_route); } if(stones){ /*for(int x = 0; x < 6; x += 4){ map<PII,PII> cur_ramp; if(x){ cur_ramp[{x - 1, 1}] = {x, 1}; } PII cur = {x, 0}; vector<PII> cur_route = {cur}; right(cur, cur_route); down(cur, cur_route); left(cur, cur_route); military_routes.push_back(cur_route); military_ramps.push_back(cur_ramp); }*/ for(int x = 0; x < n; x += 2){ if(x == n - 1){ x = n - 2; } vector<PII> cur_route; map<PII, PII> cur_ramp; PII cur = {0,x}; cur_route.push_back(cur); if(x){ cur_ramp[{0, x - 1}] = {0,x}; } right(cur, cur_route); down_left_up(cur, cur_route); military_routes.push_back(cur_route); military_ramps.push_back(cur_ramp); } { vector<PII> cur_route; PII cur = {2, 1}; cur_route.push_back(cur); while(cur.st != n - 1){ down(cur, cur_route); } left(cur,cur_route); while(cur.st != 2){ up(cur, cur_route); } map<PII, PII> cur_ramp; for(int i = n - 1; i >= 2; i--){ cur_ramp[{n - 1, i}] = {n - 1, i - 1}; } military_routes.push_back(cur_route); military_ramps.push_back(cur_ramp); } } } void down(PII & cur, vector<PII> & cur_route){ cur.st++; cur_route.PB(cur); } void up(PII & cur, vector<PII> & cur_route){ cur.st--; cur_route.PB(cur); } void left(PII & cur, vector<PII> & cur_route){ cur.nd--; cur_route.PB(cur); } void right(PII & cur, vector<PII> & cur_route){ cur.nd++; cur_route.PB(cur); } void down_left_up(PII & cur, vector<PII> & cur_route){ while(cur.st != n - 1){ down(cur, cur_route); } left(cur, cur_route); while(cur.st != 1){ up(cur,cur_route); } } void operate(){ while(military_route_fulfilled()){ military_route_idx++; apply_military_route(military_routes[military_route_idx], military_ramps[military_route_idx]); } while(route_fullfilled()){ route_idx++; apply_route(routes[route_idx]); } bool changed = true; while(changed){ changed = false; int tank_limit = n; if(stones){ for(auto t: tanks){ if(board[t->x][t->y] >= 0) changed = t->operate() || changed; } if(!pawn_of_field[0][0] && gold >= 100){ if(tanks.empty()){ new Tank(*this); changed = true; continue; } else if(SZ(tanks) < tank_limit && farmers.size() > tanks.size()){ new Tank(*this); changed = true; continue; } } } if(route_idx != -1){ int limit = routes[route_idx].size() - 1; if(stones){ limit = min(SZ(routes[route_idx]) / 2 - 1, 2 * n - 3); } if(!pawn_of_field[0][0] && gold >= 100 && SZ(farmers) < limit){ new Farmer(*this); changed = true; continue; } for(auto p: farmers){ if(p->gold) if(!board[p->x][p->y] || p->x <= 1) changed = p->operate() || changed; } for(auto p: farmers){ if(!board[p->x][p->y] || p->x <= 1) changed = p->operate() || changed; } } } } void apply_route(vector<PII> route){ for(int i = 0; i < SZ(route); i++){ int ni = (i + 1) % SZ(route); where_to[route[i].st][route[i].nd] = route[ni]; } } void apply_military_route(vector<PII> route, map<PII, PII> ramps){ for(int i = 0; i < SZ(route); i++){ int ni = (i + 1) % SZ(route); //int x1 = route[i].st, y1 = route[i].nd; //int x2 = route[ni].st, y2 = route[ni].nd; //debug(x1, y1, x2, y2); //debug(route); //assert(abs(x1 - x2) + abs(y2 - y1) == 1); military_where_to[route[i].st][route[i].nd] = route[ni]; } for(auto p: ramps){ military_where_to[p.st.st][p.st.nd] = p.nd; } } bool next_ready(){ int n_route_idx = route_idx + 1; auto & route = routes[n_route_idx]; for(auto p : route){ if(board[p.st][p.nd] < 0){ return false; } } return true; } bool route_fullfilled(){ if(route_idx == SZ(routes) - 1) return false; if(stones){ if(!next_ready()){ return false; } } if(route_idx == -1){ return true; } if(farmers.size() < 15){ for(PII p: routes[route_idx]){ if(board[p.st][p.nd] && !pawn_of_field[p.st][p.nd]){ return false; } } return true; } int bal = 0; for(PII p: routes[route_idx]){ auto f = pawn_of_field[p.st][p.nd]; if(f && p.st > 1 && f->is_farmer()){ bal++; } } return bal > 5; } bool military_route_fulfilled(){ if(military_route_idx == SZ(military_routes) - 1) return false; if(military_route_idx == -1) return true; if(true){ for(PII p: military_routes[military_route_idx]){ if(board[p.st][p.nd] < 0 && !pawn_of_field[p.st][p.nd]){ return false; } } return true; } int bal = 0; for(PII p: military_routes[route_idx]){ if(pawn_of_field[p.st][p.nd] && p.st > 1){ bal++; } } return bal > 5; } ~Board(){ for(auto p: pawns){ delete p; } } string get_pawn(int x, int y){ if(pawn_of_field[x][y]){ return pawn_of_field[x][y]->get_string(); } return ""; } void print_board(){ debug(gold); debug(total_gold); REP(i, n){ REP(j, n){ cerr<<get_pawn(i, j)<<get_string(board[i][j])<<CLEAR<<" "; } cerr<<endl; } } bool end_turn(); }; bool Board::end_turn(){ cout<<"=\n"; for(auto p: pawns){ p->end_turn(); } turn++; return total_gold == 0; } bool Pawn::operate(){ if(turn_active > b.turn){ return false; } return true; } bool Pawn::soft_operate(){ if(turn_active > b.turn){ return false; } return true; } P & operator+=(P & lhs, const P & rhs){ lhs.st += rhs.st; lhs.nd += rhs.nd; return lhs; } bool Pawn::can_move(PII d){ d += coords; for(int a: {d.st, d.nd}){ if(!between(a, 0, b.n - 1)){ return false; } } return !b.taken[d.st][d.nd]; } void Pawn::move(PII d){ //debug(x,y); //debug(d); assert(abs(d.st - x) + abs(d.nd - y) == 1); turn_active = b.turn + 1; swap(b.pawn_of_field[x][y], b.pawn_of_field[d.st][d.nd]); cout<<"M "<<x<<" "<<y<<" "<<d.st<<" "<<d.nd<<"\n"; x = d.st; y = d.nd; coords = d; } bool Farmer::operate(){ if(!Pawn::operate()){ return false; } PII dest = b.where_to[x][y]; auto p = b.pawn_of_field[dest.st][dest.nd]; if(p){ if(!p->operate()){ return false; } } Pawn::move(dest); return true; } bool Farmer::soft_operate(){ if(!Pawn::operate()){ return false; } if(x == b.n - 1 && b.board[x][y]){ return false; } return Farmer::operate(); } bool Tank::operate(){ if(!Pawn::operate()){ return false; } PII dest = b.military_where_to[x][y]; auto p = b.pawn_of_field[dest.st][dest.nd]; if(p){ if(!p->soft_operate()){ return false; } } Pawn::move(dest); return true; } bool Tank::soft_operate(){ return operate(); } Pawn::Pawn(Board & _b): x(coords.st), y(coords.nd), b(_b), turn_active(0) { b.gold -= 100; x = y = 0; b.pawn_of_field[x][y] = this; b.taken[x][y] = true; b.pawns.push_back(this); } bool Pawn::in_base(){ return x == 0 && y == 0; } Farmer::Farmer(Board & _b): Pawn(_b){ b.farmers.push_back(this); cout<<"R FARMER\n"; gold = 0; } bool Farmer::can_move(PII d){ if(!Pawn::can_move(d)){ return false; } return b.board[x][y] >= 0; } string Farmer::get_string(){ if(gold) return BLUE; else return GREEN; } void Farmer::end_turn(){ if(x == 0 && y == 0){ b.gold += gold; b.total_gold -= gold; gold = 0; } if(b.board[x][y] > 0){ int & field = b.board[x][y]; int taken = min(10, field); gold += taken; field -= taken; } } Tank::Tank(Board & _b): Pawn(_b){ b.tanks.push_back(this); cout<<"R TANK\n"; } string Tank::get_string(){ return RED; } void Tank::end_turn(){ if(b.board[x][y] < 0){ int & field = b.board[x][y]; int taken = min(10, -field); field += taken; } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int t, k; cin>>t>>k; int total = 0; REP(_, t){ debug(t); int n; cin>>n; Board b(n); //cerr << "\033[2J"; //debug(_); while(true){ b.operate(); //cerr << "\033[1;1H"; //b.print_board(); if(b.end_turn()){ break; } } cout<<"===\n"; total += b.turn; } debug(total / t); }
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 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 | #include "bits/stdc++.h" using namespace std; #define PB push_back #define LL long long #define FOR(i,a,b) for (int i = (a); i <= (b); i++) #define FORD(i,a,b) for (int i = (a); i >= (b); i--) #define REP(i,n) FOR(i,0,(int)(n)-1) #define st first #define nd second #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define VI vector<int> #define PII pair<int,int> #define LD long double #define X real() #define Y imag() 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<<"\n";} 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 string RED = "\033[41m"; const string GREEN = "\033[42;30m"; const string BLUE = "\033[44m"; const string CLEAR = "\033[0m"; typedef PII P; bool between(int m, int l, int r){ return m >= l && m <= r; } string get_string(int a){ string s = to_string(a); s = string(4 - SZ(s), ' ') + s; return s; } enum Farmer_State{ DEPOSITING, RETURNING, MINING }; struct Board; struct Farmer; struct Pawn{ P coords; int32_t & x, y; Board & b; int turn_active; int lane; Pawn(Board & _b); virtual ~Pawn(){}; virtual void end_turn() = 0; virtual bool can_move(PII d); virtual void move(PII d); virtual string get_string() = 0; bool in_base(); virtual bool operate(); virtual bool soft_operate(); virtual bool is_farmer(){ return false; } }; struct Farmer: public Pawn{ int gold; Farmer(Board & _b); bool can_move(PII d); string get_string(); void end_turn(); bool operate(); bool soft_operate(); bool is_farmer(){ return true; } }; struct Tank : public Pawn{ Tank(Board & _b); string get_string(); void end_turn(); bool operate(); bool soft_operate(); }; struct Board{ int n; vector<VI> board; vector<vector<int>> taken; vector<Tank*> tanks; vector<Farmer*> farmers; vector<Pawn*> pawns; vector<vector<Pawn*>> pawn_of_field; vector<vector<PII>> where_to; vector<vector<PII>> military_where_to; vector<vector<PII>> routes; vector<vector<PII>> military_routes; vector<map<PII, PII>> military_ramps; int route_idx; int military_route_idx; bool stones; int gold; int total_gold; int turn; Board(int _n): n(_n), board(n, VI(n)), taken(n, vector<int>(n)), pawn_of_field(n, vector<Pawn*>(n, 0)), where_to(n, vector<PII>(n)), military_where_to(n, vector<PII>(n)), stones(false), gold(200), total_gold(0), turn(0) { REP(i, n){ REP(j, n){ cin>>board[i][j]; if(board[i][j] < 0){ stones = true; } else{ total_gold += board[i][j]; } } } route_idx = -1; military_route_idx = -1; for(int y = 2; y < n; y += 2){ int x = 0; PII cur = {0,0}; vector<PII> cur_route = {cur}; while(cur.nd != x + 1){ cur.nd++; cur_route.PB(cur); } while(cur.st != y){ cur.st++; cur_route.PB(cur); } cur.nd--; cur_route.PB(cur); while(cur.st != 1){ cur.st--; cur_route.PB(cur); } while(cur.nd != 0){ cur.nd--; cur_route.PB(cur); } routes.PB(cur_route); } for(int x = 0; x < n; x += 2){ PII cur = {0,0}; vector<PII> cur_route = {cur}; if(x == n - 1){ x = n - 2; } while(cur.nd != x + 1){ cur.nd++; cur_route.PB(cur); } while(cur.st != n - 1){ cur.st++; cur_route.PB(cur); } cur.nd--; cur_route.PB(cur); while(cur.st != 1){ cur.st--; cur_route.PB(cur); } while(cur.nd != 0){ cur.nd--; cur_route.PB(cur); } routes.PB(cur_route); } if(stones){ /*for(int x = 0; x < 6; x += 4){ map<PII,PII> cur_ramp; if(x){ cur_ramp[{x - 1, 1}] = {x, 1}; } PII cur = {x, 0}; vector<PII> cur_route = {cur}; right(cur, cur_route); down(cur, cur_route); left(cur, cur_route); military_routes.push_back(cur_route); military_ramps.push_back(cur_ramp); }*/ for(int x = 0; x < n; x += 2){ if(x == n - 1){ x = n - 2; } vector<PII> cur_route; map<PII, PII> cur_ramp; PII cur = {0,x}; cur_route.push_back(cur); if(x){ cur_ramp[{0, x - 1}] = {0,x}; } right(cur, cur_route); down_left_up(cur, cur_route); military_routes.push_back(cur_route); military_ramps.push_back(cur_ramp); } { vector<PII> cur_route; PII cur = {2, 1}; cur_route.push_back(cur); while(cur.st != n - 1){ down(cur, cur_route); } left(cur,cur_route); while(cur.st != 2){ up(cur, cur_route); } map<PII, PII> cur_ramp; for(int i = n - 1; i >= 2; i--){ cur_ramp[{n - 1, i}] = {n - 1, i - 1}; } military_routes.push_back(cur_route); military_ramps.push_back(cur_ramp); } } } void down(PII & cur, vector<PII> & cur_route){ cur.st++; cur_route.PB(cur); } void up(PII & cur, vector<PII> & cur_route){ cur.st--; cur_route.PB(cur); } void left(PII & cur, vector<PII> & cur_route){ cur.nd--; cur_route.PB(cur); } void right(PII & cur, vector<PII> & cur_route){ cur.nd++; cur_route.PB(cur); } void down_left_up(PII & cur, vector<PII> & cur_route){ while(cur.st != n - 1){ down(cur, cur_route); } left(cur, cur_route); while(cur.st != 1){ up(cur,cur_route); } } void operate(){ while(military_route_fulfilled()){ military_route_idx++; apply_military_route(military_routes[military_route_idx], military_ramps[military_route_idx]); } while(route_fullfilled()){ route_idx++; apply_route(routes[route_idx]); } bool changed = true; while(changed){ changed = false; int tank_limit = n; if(stones){ for(auto t: tanks){ if(board[t->x][t->y] >= 0) changed = t->operate() || changed; } if(!pawn_of_field[0][0] && gold >= 100){ if(tanks.empty()){ new Tank(*this); changed = true; continue; } else if(SZ(tanks) < tank_limit && farmers.size() > tanks.size()){ new Tank(*this); changed = true; continue; } } } if(route_idx != -1){ int limit = routes[route_idx].size() - 1; if(stones){ limit = min(SZ(routes[route_idx]) / 2 - 1, 2 * n - 3); } if(!pawn_of_field[0][0] && gold >= 100 && SZ(farmers) < limit){ new Farmer(*this); changed = true; continue; } for(auto p: farmers){ if(p->gold) if(!board[p->x][p->y] || p->x <= 1) changed = p->operate() || changed; } for(auto p: farmers){ if(!board[p->x][p->y] || p->x <= 1) changed = p->operate() || changed; } } } } void apply_route(vector<PII> route){ for(int i = 0; i < SZ(route); i++){ int ni = (i + 1) % SZ(route); where_to[route[i].st][route[i].nd] = route[ni]; } } void apply_military_route(vector<PII> route, map<PII, PII> ramps){ for(int i = 0; i < SZ(route); i++){ int ni = (i + 1) % SZ(route); //int x1 = route[i].st, y1 = route[i].nd; //int x2 = route[ni].st, y2 = route[ni].nd; //debug(x1, y1, x2, y2); //debug(route); //assert(abs(x1 - x2) + abs(y2 - y1) == 1); military_where_to[route[i].st][route[i].nd] = route[ni]; } for(auto p: ramps){ military_where_to[p.st.st][p.st.nd] = p.nd; } } bool next_ready(){ int n_route_idx = route_idx + 1; auto & route = routes[n_route_idx]; for(auto p : route){ if(board[p.st][p.nd] < 0){ return false; } } return true; } bool route_fullfilled(){ if(route_idx == SZ(routes) - 1) return false; if(stones){ if(!next_ready()){ return false; } } if(route_idx == -1){ return true; } if(farmers.size() < 15){ for(PII p: routes[route_idx]){ if(board[p.st][p.nd] && !pawn_of_field[p.st][p.nd]){ return false; } } return true; } int bal = 0; for(PII p: routes[route_idx]){ auto f = pawn_of_field[p.st][p.nd]; if(f && p.st > 1 && f->is_farmer()){ bal++; } } return bal > 5; } bool military_route_fulfilled(){ if(military_route_idx == SZ(military_routes) - 1) return false; if(military_route_idx == -1) return true; if(true){ for(PII p: military_routes[military_route_idx]){ if(board[p.st][p.nd] < 0 && !pawn_of_field[p.st][p.nd]){ return false; } } return true; } int bal = 0; for(PII p: military_routes[route_idx]){ if(pawn_of_field[p.st][p.nd] && p.st > 1){ bal++; } } return bal > 5; } ~Board(){ for(auto p: pawns){ delete p; } } string get_pawn(int x, int y){ if(pawn_of_field[x][y]){ return pawn_of_field[x][y]->get_string(); } return ""; } void print_board(){ debug(gold); debug(total_gold); REP(i, n){ REP(j, n){ cerr<<get_pawn(i, j)<<get_string(board[i][j])<<CLEAR<<" "; } cerr<<endl; } } bool end_turn(); }; bool Board::end_turn(){ cout<<"=\n"; for(auto p: pawns){ p->end_turn(); } turn++; return total_gold == 0; } bool Pawn::operate(){ if(turn_active > b.turn){ return false; } return true; } bool Pawn::soft_operate(){ if(turn_active > b.turn){ return false; } return true; } P & operator+=(P & lhs, const P & rhs){ lhs.st += rhs.st; lhs.nd += rhs.nd; return lhs; } bool Pawn::can_move(PII d){ d += coords; for(int a: {d.st, d.nd}){ if(!between(a, 0, b.n - 1)){ return false; } } return !b.taken[d.st][d.nd]; } void Pawn::move(PII d){ //debug(x,y); //debug(d); assert(abs(d.st - x) + abs(d.nd - y) == 1); turn_active = b.turn + 1; swap(b.pawn_of_field[x][y], b.pawn_of_field[d.st][d.nd]); cout<<"M "<<x<<" "<<y<<" "<<d.st<<" "<<d.nd<<"\n"; x = d.st; y = d.nd; coords = d; } bool Farmer::operate(){ if(!Pawn::operate()){ return false; } PII dest = b.where_to[x][y]; auto p = b.pawn_of_field[dest.st][dest.nd]; if(p){ if(!p->operate()){ return false; } } Pawn::move(dest); return true; } bool Farmer::soft_operate(){ if(!Pawn::operate()){ return false; } if(x == b.n - 1 && b.board[x][y]){ return false; } return Farmer::operate(); } bool Tank::operate(){ if(!Pawn::operate()){ return false; } PII dest = b.military_where_to[x][y]; auto p = b.pawn_of_field[dest.st][dest.nd]; if(p){ if(!p->soft_operate()){ return false; } } Pawn::move(dest); return true; } bool Tank::soft_operate(){ return operate(); } Pawn::Pawn(Board & _b): x(coords.st), y(coords.nd), b(_b), turn_active(0) { b.gold -= 100; x = y = 0; b.pawn_of_field[x][y] = this; b.taken[x][y] = true; b.pawns.push_back(this); } bool Pawn::in_base(){ return x == 0 && y == 0; } Farmer::Farmer(Board & _b): Pawn(_b){ b.farmers.push_back(this); cout<<"R FARMER\n"; gold = 0; } bool Farmer::can_move(PII d){ if(!Pawn::can_move(d)){ return false; } return b.board[x][y] >= 0; } string Farmer::get_string(){ if(gold) return BLUE; else return GREEN; } void Farmer::end_turn(){ if(x == 0 && y == 0){ b.gold += gold; b.total_gold -= gold; gold = 0; } if(b.board[x][y] > 0){ int & field = b.board[x][y]; int taken = min(10, field); gold += taken; field -= taken; } } Tank::Tank(Board & _b): Pawn(_b){ b.tanks.push_back(this); cout<<"R TANK\n"; } string Tank::get_string(){ return RED; } void Tank::end_turn(){ if(b.board[x][y] < 0){ int & field = b.board[x][y]; int taken = min(10, -field); field += taken; } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int t, k; cin>>t>>k; int total = 0; REP(_, t){ debug(t); int n; cin>>n; Board b(n); //cerr << "\033[2J"; //debug(_); while(true){ b.operate(); //cerr << "\033[1;1H"; //b.print_board(); if(b.end_turn()){ break; } } cout<<"===\n"; total += b.turn; } debug(total / t); } |