#include <bits/stdc++.h> #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 VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define all(v) (v).begin(),(v).end() #define PII pair<int,int> #define mp make_pair #define st first #define nd second #define pb push_back #define lint long long int #define VI vector<int> #define debug(x) {cout <<#x <<" = " <<x <<endl; } #define debug2(x,y) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y <<endl; } #define debug3(x,y,z) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y << ", " << #z << " = " << z <<endl; } #define debugv(x) {{cout <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }} #define debugt(t,n) {{cerr <<#t <<" = "; FOR(it,0,(n)) cerr <<t[it] <<", "; cerr <<endl; }} #define make( x) int (x); scanf("%d",&(x)); #define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y)); #define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z)); #define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define makev(v,n) VI (v); FOR(i,0,(n)) { make(a); (v).pb(a);} #define IOS ios_base::sync_with_stdio(0) #define HEAP priority_queue #define read( x) scanf("%d",&(x)); #define read2( x, y) scanf("%d%d",&(x),&(y)); #define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z)); #define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define readv(v,n) FOR(i,0,(n)) { make(a); (v).pb(a);} #define jeb() fflush(stdout); using namespace std; int T,k,n = 20; double p = 0.; int A[25][25]; int kto[25][25]; vector<pair<PII,int> > farmerzy; vector<PII> tanki; int sa_zle; int HAJS; int farmerzy_w_bazie = 0; int tanki_w_bazie = 0; VI ruchy_poza; int ZACZELO_SIE = 0; int DOSZEDLEM_DO_KONCA[25]; int MOJ_WIERSZ[25]; int ZEBRALI = 0; int KOLEJNOSC[25]; PII MOJE[25]; void zeruj_zmienne() { farmerzy.clear(); sa_zle = 0; HAJS = 0; farmerzy_w_bazie = 0; tanki_w_bazie = 0; ruchy_poza.clear(); ZACZELO_SIE = 0; FOR(i,0,23) FOR(j,0,23) kto[i][j] = 0; FOR(i,0,23) DOSZEDLEM_DO_KONCA[i] = 0; FOR(i,0,23) MOJ_WIERSZ[i] = -1; FOR(i,0,23) KOLEJNOSC[i] = -1; ZEBRALI = 0; FOR(i,0,23) MOJE[i] = mp(-1,-1);; } void wczytaj() { read(n); sa_zle = 0; FOR(i,0,n) { FOR(j,0,n) { make(a); A[i][j] = a; if (a < 0) sa_zle++; } } p = sa_zle*1./(n*n); HAJS = 200; } bool kup_farmera() { if (HAJS >= 100 && kto[0][0] == 0 && farmerzy.size() < 20) { printf("R FARMER\n"); HAJS -= 100; farmerzy.pb({{0,0},0}); kto[0][0] = farmerzy.size(); ruchy_poza.pb(0); farmerzy_w_bazie++; return true; } return false; } bool kup_tanka() { if (HAJS >= 100 && kto[0][0] == 0 ) { printf("R TANK\n"); HAJS -= 100; tanki.pb({0,0}); kto[0][0] = -tanki.size() -1 ; tanki_w_bazie++; return true; } return false; } bool sprawdz() { FOR(i,0,n) FOR(j,0,n) if (A[i][j] > 0) return false; FORE(i, farmerzy) { if (i->st != mp(0,0) && i->nd != 0) return false; } return true; } void rusz_farmera(int ja, int c, int d) { if (c < 0 || d < 0 || c == n || d == n) return; if (kto[c][d] != 0) { return; } if (A[c][d] < 0) return; int mam_zlota = farmerzy[ja].nd; int a = farmerzy[ja].st.st; int b = farmerzy[ja].st.nd; kto[a][b] = 0; if (a==0 && b==0) farmerzy_w_bazie--; printf("M %d %d %d %d\n", a, b, c, d); if (c == 0 && d == 0) { kto[c][d] = ja+1; HAJS += mam_zlota; farmerzy_w_bazie++; farmerzy[ja] = {{0,0},0}; ruchy_poza[ja] = 0; } else { kto[c][d] = ja+1; farmerzy[ja] = {{c,d},mam_zlota}; } } void aktualizuj() { FOR(i,0,tanki.size()) { int x = tanki[i].st; int y =tanki[i].nd; if (A[x][y] < 0) { int niszcz = min(-A[x][y], 10); A[x][y] += niszcz; } } FOR(i,0,farmerzy.size()) { int x = farmerzy[i].st.st; int y = farmerzy[i].st.nd; int mam = farmerzy[i].nd; int zbior = min(A[x][y], 10); A[x][y] -= zbior; farmerzy[i] = {{x,y},mam + zbior}; if (x == 0 && y == 0) { ruchy_poza[i] = 0; } else { ruchy_poza[i]++; } } } void rusz_tanka(int ja, int c, int d) { if (c < 0 || d < 0 || c == n || d == n) return; if (kto[c][d] != 0) return; int a = tanki[ja].st; int b = tanki[ja].nd; kto[a][b] = 0; printf("M %d %d %d %d\n", a, b, c, d); if (c == 0 && d == 0) { tanki[ja] = {0,0}; kto[c][d] = -ja-2; } else { kto[c][d] = -ja-2; tanki[ja] = {c,d}; } } void wypisz() { FOR(i,0,n) { FOR(j,0,n) { char C = ' '; if (kto[i][j] < 0) C = 'T'; if (kto[i][j] > 0) C = 'F'; printf("%4.d|(%c%2.d) ", A[i][j], C,abs(kto[i][j])); } cout << endl; } cout << "OGOLEM MAM "<<farmerzy.size() << endl; cout << "FARMERS: " << farmerzy_w_bazie << " " << "\nTANKS: "<<tanki_w_bazie << "\nHAJS: "<<HAJS << endl; } int dx[] = {-1,0,1,0}; int dy[] = {0,-1,0,1}; int wracaj_farmerze(int ja) { int co = 0; int x = farmerzy[ja].st.st; int y = farmerzy[ja].st.nd; bool ok = true; int nx = x + dx[co], ny = y + dy[co]; if (nx < 0 || ny < 0 || nx == n || ny == n) ok = false; if (!ok) { ok = true; co = 1-co; nx = x + dx[co], ny = y + dy[co]; if (nx < 0 || ny < 0 || nx == n || ny == n) return 0; } if (kto[nx][ny] != 0) ok = false; if (ok) { rusz_farmera(ja,x+dx[co], y+dy[co]); return 1; } } void idz_w_swiat(int ja) { int x = farmerzy[ja].st.st; int y = farmerzy[ja].st.nd; int ruch = rand()%2 + 2; FOR(i,0,2) { int nx = x + dx[ruch]; int ny = y + dy[ruch]; bool ok = true; if (nx < 0 || ny < 0 || nx == n || ny == n) ok = false; if (kto[nx][ny] != 0) ok = false; if (ok) { rusz_farmera(ja, x + dx[ruch], y+dy[ruch]); return; } ok = true; ruch = 5-ruch; } } void losuj_farmerze(int ja) { int x = farmerzy[ja].st.st; int y = farmerzy[ja].st.nd; FOR(i,0,8) { int ruch = rand()%4; int nx = x + dx[ruch]; int ny = y + dy[ruch]; bool ok = true; if (nx < 0 || ny < 0 || nx == n || ny == n) ok = false; if (kto[nx][ny] != 0) ok = false; if (ok) { rusz_farmera(ja, nx, ny); return; } ok = true; } } void wybierz_max_z_dostepnych(int ja) { int x = farmerzy[ja].st.st; int y = farmerzy[ja].st.nd; PII dla = {x,y}; int ile = A[x][y]; FOR(i,0,4) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx < 0 || ny < 0 || nx >= n || ny >= n) continue; if (kto[nx][ny] != 0) continue; if (A[nx][ny] > ile) { ile = A[nx][ny]; dla = {nx, ny}; } } if (dla != mp(x,y)) { rusz_farmera(ja, dla.st, dla.nd); } else if (ile == 0) { wracaj_farmerze(ja); } } void tura_bez_tankow(int nr) { /*cout << "POCZATEK " << nr << "TURY" << endl; wypisz(); getchar();*/ if (farmerzy.size() < 20) kup_farmera(); int FARMEROW = farmerzy.size(); if (ZEBRALI == 20) { FOR(i,0,20) KOLEJNOSC[MOJ_WIERSZ[i]] = i; FOR(i,0,20) { int ja = KOLEJNOSC[i]; int x = farmerzy[ja].st.st; int y = farmerzy[ja].st.nd; if (x == 0 && y == 0) { rusz_farmera(ja, 0, 1); } else if (y == 1) rusz_farmera(ja, x+1, y); else rusz_farmera(ja, x-1, y); } } else { for (int i = 0; i < farmerzy.size(); i++) { if (farmerzy.size() < 20) { int BND = 250; if (FARMEROW < 5) BND = 250; else BND = 100; int W_SWIAT = 10; if (FARMEROW < 5) W_SWIAT = 10; else W_SWIAT = 15; if (ruchy_poza[i] < W_SWIAT) { idz_w_swiat(i); } else if (farmerzy[i].nd < BND) { wybierz_max_z_dostepnych(i); } else { wracaj_farmerze(i); } } else if (!ZACZELO_SIE) { FOR(i,0,20) MOJ_WIERSZ[i] = i; FOR(a,0,20) { FOR(i,0,20) { FOR(j,0,20) { if (farmerzy[i].st.st < farmerzy[j].st.st && MOJ_WIERSZ[i] > MOJ_WIERSZ[j]) swap(MOJ_WIERSZ[i], MOJ_WIERSZ[j]); } } } ZACZELO_SIE = 1; } else if (ZACZELO_SIE) { if (i >= 20) continue; if (DOSZEDLEM_DO_KONCA[i] == 0) { int x = farmerzy[i].st.st; int y = farmerzy[i].st.nd; if (x > MOJ_WIERSZ[i]) { rusz_farmera(i, x-1, y); } else if (x < MOJ_WIERSZ[i]) { rusz_farmera(i, x+1, y); } else { rusz_farmera(i, x , y+1 ); } if (farmerzy[i].st.st == MOJ_WIERSZ[i] && farmerzy[i].st.nd == n-1) DOSZEDLEM_DO_KONCA[i] = 1; } else if (DOSZEDLEM_DO_KONCA[i] == 1) { int x = farmerzy[i].st.st; int y = farmerzy[i].st.nd; if (A[x][y] > 0) continue; else { if ( (y == 0 && DOSZEDLEM_DO_KONCA[i] == 1)) { DOSZEDLEM_DO_KONCA[i] = 2; ZEBRALI++; } else if (y > 0) { rusz_farmera(i,x,y-1); } } } } } } aktualizuj(); } void idz_tanku_w_swiat(int ja) { int x = tanki[ja].st; int y = tanki[ja].nd; int ruch = rand()%2 + 2; FOR(i,0,2) { int nx = x + dx[ruch]; int ny = y + dy[ruch]; bool ok = true; if (nx < 0 || ny < 0 || nx == n || ny == n) ok = false; if (kto[nx][ny] != 0) ok = false; if (ok) { rusz_tanka(ja, x + dx[ruch], y+dy[ruch]); return; } ok = true; ruch = 5-ruch; } } int idz_tanku_na_ujemnego_sasiada(int ja ) { int x = tanki[ja].st; int y = tanki[ja].nd; FOR(i,0,4) { int ruch = rand()% 4; int nx = x + dx[ruch]; int ny = y + dy[ruch]; bool ok = true; if (nx < 0 || ny < 0 || nx == n || ny == n) ok = false; if (kto[nx][ny] != 0) ok = false; if (A[nx][ny] >= 0) ok = false; if (ok) { rusz_tanka(ja, nx, ny); return 1; } ok = true; } return 0; } void idz_tanku_losowo(int ja) { int x = tanki[ja].st; int y = tanki[ja].nd; FOR(i,0,4) { int ruch = rand()% 4; int nx = x + dx[ruch]; int ny = y + dy[ruch]; bool ok = true; if (nx < 0 || ny < 0 || nx == n || ny == n) ok = false; if (kto[nx][ny] != 0) ok = false; if (ok) { rusz_tanka(ja, nx, ny); return; } ok = true; } } PII znajdz_przeszkode(int x, int y) { VI v, w; FOR(i,0,x) v.pb(i); FOR(i,0,y) w.pb(i); random_shuffle(all(v)); random_shuffle(all(w)); FOR(i,0,x) FOR(j,0,y) { if (A[v[i]][w[j]] < 0 && kto[v[i]][w[j]] == 0) return mp(v[i],w[j]); } return mp(-1,-1); } PII znajdz_zloto() { VI v, w; FOR(i,0,20) v.pb(i); FOR(i,0,20) w.pb(i); random_shuffle(all(v)); random_shuffle(all(w)); FOR(i,0,n) FOR(j,0,n) { if (A[v[i]][w[j]] > 0 && kto[v[i]][w[j]] == 0) return mp(v[i],w[j]); } return mp(-1,-1); } void tura_z_tankami(int nr) { /*cout << "POCZATEK " << nr << "TURY" << endl; wypisz(); getchar();*/ double BND = 0.5; if (sa_zle > 350) BND = 0.6; if (sa_zle > 450) BND = 0.7; if (sa_zle > 500) BND = 0.8; if (sa_zle > 650) BND = 1.5; if (nr == 0) kup_tanka(); else if (nr == 1) kup_farmera(); else { if (tanki.size() < BND*farmerzy.size() && tanki.size() < 20) kup_tanka(); if (farmerzy.size() < 20) kup_farmera(); if (farmerzy.size() == 20 && tanki.size()<25) kup_tanka(); } FOR(i,0,tanki.size()) { if (farmerzy.size() < 20) { int x = tanki[i].st, y = tanki[i].nd; if (A[x][y] < 0) continue; PII prz = znajdz_przeszkode(5,5); if (prz != mp(-1,-1)) { if (rand()%4 == 0) { idz_tanku_losowo(i); continue; } if (x < prz.st) rusz_tanka(i,x+1,y); else if (x > prz.st) rusz_tanka(i,x-1,y); else if (y > prz.nd) rusz_tanka(i,x,y-1); else if (y < prz.nd) rusz_tanka(i,x,y+1); continue; } if (x + y < 7) { idz_tanku_w_swiat(i); continue; } else if(idz_tanku_na_ujemnego_sasiada(i)) { continue; } else { idz_tanku_losowo(i); continue; } } else { int x = tanki[i].st, y = tanki[i].nd; if (A[x][y] < 0) continue; PII prz = znajdz_przeszkode(20,20); if (prz != mp(-1,-1)) { if (x < prz.st) { rusz_tanka(i,x+1,y); continue;} else if (x > prz.st) {rusz_tanka(i,x-1,y); continue;} else if (y > prz.nd) {rusz_tanka(i,x,y-1); continue;} else if (y < prz.nd) {rusz_tanka(i,x,y+1); continue;} continue; } if (x + y < 7) { idz_tanku_w_swiat(i); continue; } else if(idz_tanku_na_ujemnego_sasiada(i)) { continue; } else { idz_tanku_losowo(i); continue; } } } int FARMEROW = farmerzy.size(); FOR(i,0,farmerzy.size()) { if (farmerzy.size() < 5) { if (rand()%7 == 0) { losuj_farmerze(i); continue; } if (farmerzy[i].nd >= 100) { if (wracaj_farmerze(i)) { continue; } else { losuj_farmerze(i); continue; } } else { if (ruchy_poza[i] < 6) { idz_w_swiat(i); continue; } else if (rand()%2) { wybierz_max_z_dostepnych(i); continue; } else { wracaj_farmerze(i); continue; } } } else if (farmerzy.size() < 20) { if (rand()%6 == 0 && farmerzy[i].st.st + farmerzy[i].st.nd < 22) { idz_w_swiat(i); continue; } else if (rand()%6 == 0) { losuj_farmerze(i); continue; } int BND = 100; int W_SWIAT = 15; if (farmerzy[i].nd >= 200) { if (wracaj_farmerze(i)) { continue; } else { losuj_farmerze(i); continue; } } else { if (ruchy_poza[i] < W_SWIAT) { idz_w_swiat(i); continue; } else if (rand()%3) { wybierz_max_z_dostepnych(i); continue; } else { wracaj_farmerze(i); continue; } } } else { int x = farmerzy[i].st.st; int y = farmerzy[i].st.nd; if (A[x][y] > 0) continue; if (MOJE[i] != mp(-1,-1) && A[MOJE[i].st][MOJE[i].nd] == 0) { MOJE[i] = mp(-1,-1); } if (MOJE[i] == mp(-1,-1)) { MOJE[i] = znajdz_zloto(); } if (MOJE[i] != mp(-1,-1)) { if (rand()%5 == 0) { losuj_farmerze(i); continue; } else { PII zloto = MOJE[i]; if (zloto.st < x) { rusz_farmera(i, x-1, y); continue; } else if (zloto.st > x) { rusz_farmera(i, x+1, y); continue; } else if (zloto.nd > y) { rusz_farmera(i, x, y+1); continue; } else if (zloto.nd < y) { rusz_farmera(i, x, y-1); continue; } else { losuj_farmerze(i); continue; } } } else { if (farmerzy[i].nd > 0) { if (rand()%4) wracaj_farmerze(i); else losuj_farmerze(i); } else { if (x+y<7) idz_w_swiat(i); else losuj_farmerze(i); } } } } aktualizuj(); } int graj() { zeruj_zmienne(); wczytaj(); int licz = 0; if (!sa_zle) { while (1) { tura_bez_tankow(licz); licz++; printf("=\n"); if (sprawdz()) { printf("===\n"); break; } } return licz; } else { while (1) { tura_z_tankami(licz); licz++; printf("=\n"); if (sprawdz()) { printf("===\n"); break; } } } return licz; } int main() { srand(time(NULL)); read2(T, k); int res = 0; int suma = 0; FOR(i,0,T) { suma += graj(); } }
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 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 | #include <bits/stdc++.h> #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 VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define all(v) (v).begin(),(v).end() #define PII pair<int,int> #define mp make_pair #define st first #define nd second #define pb push_back #define lint long long int #define VI vector<int> #define debug(x) {cout <<#x <<" = " <<x <<endl; } #define debug2(x,y) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y <<endl; } #define debug3(x,y,z) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y << ", " << #z << " = " << z <<endl; } #define debugv(x) {{cout <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }} #define debugt(t,n) {{cerr <<#t <<" = "; FOR(it,0,(n)) cerr <<t[it] <<", "; cerr <<endl; }} #define make( x) int (x); scanf("%d",&(x)); #define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y)); #define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z)); #define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define makev(v,n) VI (v); FOR(i,0,(n)) { make(a); (v).pb(a);} #define IOS ios_base::sync_with_stdio(0) #define HEAP priority_queue #define read( x) scanf("%d",&(x)); #define read2( x, y) scanf("%d%d",&(x),&(y)); #define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z)); #define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define readv(v,n) FOR(i,0,(n)) { make(a); (v).pb(a);} #define jeb() fflush(stdout); using namespace std; int T,k,n = 20; double p = 0.; int A[25][25]; int kto[25][25]; vector<pair<PII,int> > farmerzy; vector<PII> tanki; int sa_zle; int HAJS; int farmerzy_w_bazie = 0; int tanki_w_bazie = 0; VI ruchy_poza; int ZACZELO_SIE = 0; int DOSZEDLEM_DO_KONCA[25]; int MOJ_WIERSZ[25]; int ZEBRALI = 0; int KOLEJNOSC[25]; PII MOJE[25]; void zeruj_zmienne() { farmerzy.clear(); sa_zle = 0; HAJS = 0; farmerzy_w_bazie = 0; tanki_w_bazie = 0; ruchy_poza.clear(); ZACZELO_SIE = 0; FOR(i,0,23) FOR(j,0,23) kto[i][j] = 0; FOR(i,0,23) DOSZEDLEM_DO_KONCA[i] = 0; FOR(i,0,23) MOJ_WIERSZ[i] = -1; FOR(i,0,23) KOLEJNOSC[i] = -1; ZEBRALI = 0; FOR(i,0,23) MOJE[i] = mp(-1,-1);; } void wczytaj() { read(n); sa_zle = 0; FOR(i,0,n) { FOR(j,0,n) { make(a); A[i][j] = a; if (a < 0) sa_zle++; } } p = sa_zle*1./(n*n); HAJS = 200; } bool kup_farmera() { if (HAJS >= 100 && kto[0][0] == 0 && farmerzy.size() < 20) { printf("R FARMER\n"); HAJS -= 100; farmerzy.pb({{0,0},0}); kto[0][0] = farmerzy.size(); ruchy_poza.pb(0); farmerzy_w_bazie++; return true; } return false; } bool kup_tanka() { if (HAJS >= 100 && kto[0][0] == 0 ) { printf("R TANK\n"); HAJS -= 100; tanki.pb({0,0}); kto[0][0] = -tanki.size() -1 ; tanki_w_bazie++; return true; } return false; } bool sprawdz() { FOR(i,0,n) FOR(j,0,n) if (A[i][j] > 0) return false; FORE(i, farmerzy) { if (i->st != mp(0,0) && i->nd != 0) return false; } return true; } void rusz_farmera(int ja, int c, int d) { if (c < 0 || d < 0 || c == n || d == n) return; if (kto[c][d] != 0) { return; } if (A[c][d] < 0) return; int mam_zlota = farmerzy[ja].nd; int a = farmerzy[ja].st.st; int b = farmerzy[ja].st.nd; kto[a][b] = 0; if (a==0 && b==0) farmerzy_w_bazie--; printf("M %d %d %d %d\n", a, b, c, d); if (c == 0 && d == 0) { kto[c][d] = ja+1; HAJS += mam_zlota; farmerzy_w_bazie++; farmerzy[ja] = {{0,0},0}; ruchy_poza[ja] = 0; } else { kto[c][d] = ja+1; farmerzy[ja] = {{c,d},mam_zlota}; } } void aktualizuj() { FOR(i,0,tanki.size()) { int x = tanki[i].st; int y =tanki[i].nd; if (A[x][y] < 0) { int niszcz = min(-A[x][y], 10); A[x][y] += niszcz; } } FOR(i,0,farmerzy.size()) { int x = farmerzy[i].st.st; int y = farmerzy[i].st.nd; int mam = farmerzy[i].nd; int zbior = min(A[x][y], 10); A[x][y] -= zbior; farmerzy[i] = {{x,y},mam + zbior}; if (x == 0 && y == 0) { ruchy_poza[i] = 0; } else { ruchy_poza[i]++; } } } void rusz_tanka(int ja, int c, int d) { if (c < 0 || d < 0 || c == n || d == n) return; if (kto[c][d] != 0) return; int a = tanki[ja].st; int b = tanki[ja].nd; kto[a][b] = 0; printf("M %d %d %d %d\n", a, b, c, d); if (c == 0 && d == 0) { tanki[ja] = {0,0}; kto[c][d] = -ja-2; } else { kto[c][d] = -ja-2; tanki[ja] = {c,d}; } } void wypisz() { FOR(i,0,n) { FOR(j,0,n) { char C = ' '; if (kto[i][j] < 0) C = 'T'; if (kto[i][j] > 0) C = 'F'; printf("%4.d|(%c%2.d) ", A[i][j], C,abs(kto[i][j])); } cout << endl; } cout << "OGOLEM MAM "<<farmerzy.size() << endl; cout << "FARMERS: " << farmerzy_w_bazie << " " << "\nTANKS: "<<tanki_w_bazie << "\nHAJS: "<<HAJS << endl; } int dx[] = {-1,0,1,0}; int dy[] = {0,-1,0,1}; int wracaj_farmerze(int ja) { int co = 0; int x = farmerzy[ja].st.st; int y = farmerzy[ja].st.nd; bool ok = true; int nx = x + dx[co], ny = y + dy[co]; if (nx < 0 || ny < 0 || nx == n || ny == n) ok = false; if (!ok) { ok = true; co = 1-co; nx = x + dx[co], ny = y + dy[co]; if (nx < 0 || ny < 0 || nx == n || ny == n) return 0; } if (kto[nx][ny] != 0) ok = false; if (ok) { rusz_farmera(ja,x+dx[co], y+dy[co]); return 1; } } void idz_w_swiat(int ja) { int x = farmerzy[ja].st.st; int y = farmerzy[ja].st.nd; int ruch = rand()%2 + 2; FOR(i,0,2) { int nx = x + dx[ruch]; int ny = y + dy[ruch]; bool ok = true; if (nx < 0 || ny < 0 || nx == n || ny == n) ok = false; if (kto[nx][ny] != 0) ok = false; if (ok) { rusz_farmera(ja, x + dx[ruch], y+dy[ruch]); return; } ok = true; ruch = 5-ruch; } } void losuj_farmerze(int ja) { int x = farmerzy[ja].st.st; int y = farmerzy[ja].st.nd; FOR(i,0,8) { int ruch = rand()%4; int nx = x + dx[ruch]; int ny = y + dy[ruch]; bool ok = true; if (nx < 0 || ny < 0 || nx == n || ny == n) ok = false; if (kto[nx][ny] != 0) ok = false; if (ok) { rusz_farmera(ja, nx, ny); return; } ok = true; } } void wybierz_max_z_dostepnych(int ja) { int x = farmerzy[ja].st.st; int y = farmerzy[ja].st.nd; PII dla = {x,y}; int ile = A[x][y]; FOR(i,0,4) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx < 0 || ny < 0 || nx >= n || ny >= n) continue; if (kto[nx][ny] != 0) continue; if (A[nx][ny] > ile) { ile = A[nx][ny]; dla = {nx, ny}; } } if (dla != mp(x,y)) { rusz_farmera(ja, dla.st, dla.nd); } else if (ile == 0) { wracaj_farmerze(ja); } } void tura_bez_tankow(int nr) { /*cout << "POCZATEK " << nr << "TURY" << endl; wypisz(); getchar();*/ if (farmerzy.size() < 20) kup_farmera(); int FARMEROW = farmerzy.size(); if (ZEBRALI == 20) { FOR(i,0,20) KOLEJNOSC[MOJ_WIERSZ[i]] = i; FOR(i,0,20) { int ja = KOLEJNOSC[i]; int x = farmerzy[ja].st.st; int y = farmerzy[ja].st.nd; if (x == 0 && y == 0) { rusz_farmera(ja, 0, 1); } else if (y == 1) rusz_farmera(ja, x+1, y); else rusz_farmera(ja, x-1, y); } } else { for (int i = 0; i < farmerzy.size(); i++) { if (farmerzy.size() < 20) { int BND = 250; if (FARMEROW < 5) BND = 250; else BND = 100; int W_SWIAT = 10; if (FARMEROW < 5) W_SWIAT = 10; else W_SWIAT = 15; if (ruchy_poza[i] < W_SWIAT) { idz_w_swiat(i); } else if (farmerzy[i].nd < BND) { wybierz_max_z_dostepnych(i); } else { wracaj_farmerze(i); } } else if (!ZACZELO_SIE) { FOR(i,0,20) MOJ_WIERSZ[i] = i; FOR(a,0,20) { FOR(i,0,20) { FOR(j,0,20) { if (farmerzy[i].st.st < farmerzy[j].st.st && MOJ_WIERSZ[i] > MOJ_WIERSZ[j]) swap(MOJ_WIERSZ[i], MOJ_WIERSZ[j]); } } } ZACZELO_SIE = 1; } else if (ZACZELO_SIE) { if (i >= 20) continue; if (DOSZEDLEM_DO_KONCA[i] == 0) { int x = farmerzy[i].st.st; int y = farmerzy[i].st.nd; if (x > MOJ_WIERSZ[i]) { rusz_farmera(i, x-1, y); } else if (x < MOJ_WIERSZ[i]) { rusz_farmera(i, x+1, y); } else { rusz_farmera(i, x , y+1 ); } if (farmerzy[i].st.st == MOJ_WIERSZ[i] && farmerzy[i].st.nd == n-1) DOSZEDLEM_DO_KONCA[i] = 1; } else if (DOSZEDLEM_DO_KONCA[i] == 1) { int x = farmerzy[i].st.st; int y = farmerzy[i].st.nd; if (A[x][y] > 0) continue; else { if ( (y == 0 && DOSZEDLEM_DO_KONCA[i] == 1)) { DOSZEDLEM_DO_KONCA[i] = 2; ZEBRALI++; } else if (y > 0) { rusz_farmera(i,x,y-1); } } } } } } aktualizuj(); } void idz_tanku_w_swiat(int ja) { int x = tanki[ja].st; int y = tanki[ja].nd; int ruch = rand()%2 + 2; FOR(i,0,2) { int nx = x + dx[ruch]; int ny = y + dy[ruch]; bool ok = true; if (nx < 0 || ny < 0 || nx == n || ny == n) ok = false; if (kto[nx][ny] != 0) ok = false; if (ok) { rusz_tanka(ja, x + dx[ruch], y+dy[ruch]); return; } ok = true; ruch = 5-ruch; } } int idz_tanku_na_ujemnego_sasiada(int ja ) { int x = tanki[ja].st; int y = tanki[ja].nd; FOR(i,0,4) { int ruch = rand()% 4; int nx = x + dx[ruch]; int ny = y + dy[ruch]; bool ok = true; if (nx < 0 || ny < 0 || nx == n || ny == n) ok = false; if (kto[nx][ny] != 0) ok = false; if (A[nx][ny] >= 0) ok = false; if (ok) { rusz_tanka(ja, nx, ny); return 1; } ok = true; } return 0; } void idz_tanku_losowo(int ja) { int x = tanki[ja].st; int y = tanki[ja].nd; FOR(i,0,4) { int ruch = rand()% 4; int nx = x + dx[ruch]; int ny = y + dy[ruch]; bool ok = true; if (nx < 0 || ny < 0 || nx == n || ny == n) ok = false; if (kto[nx][ny] != 0) ok = false; if (ok) { rusz_tanka(ja, nx, ny); return; } ok = true; } } PII znajdz_przeszkode(int x, int y) { VI v, w; FOR(i,0,x) v.pb(i); FOR(i,0,y) w.pb(i); random_shuffle(all(v)); random_shuffle(all(w)); FOR(i,0,x) FOR(j,0,y) { if (A[v[i]][w[j]] < 0 && kto[v[i]][w[j]] == 0) return mp(v[i],w[j]); } return mp(-1,-1); } PII znajdz_zloto() { VI v, w; FOR(i,0,20) v.pb(i); FOR(i,0,20) w.pb(i); random_shuffle(all(v)); random_shuffle(all(w)); FOR(i,0,n) FOR(j,0,n) { if (A[v[i]][w[j]] > 0 && kto[v[i]][w[j]] == 0) return mp(v[i],w[j]); } return mp(-1,-1); } void tura_z_tankami(int nr) { /*cout << "POCZATEK " << nr << "TURY" << endl; wypisz(); getchar();*/ double BND = 0.5; if (sa_zle > 350) BND = 0.6; if (sa_zle > 450) BND = 0.7; if (sa_zle > 500) BND = 0.8; if (sa_zle > 650) BND = 1.5; if (nr == 0) kup_tanka(); else if (nr == 1) kup_farmera(); else { if (tanki.size() < BND*farmerzy.size() && tanki.size() < 20) kup_tanka(); if (farmerzy.size() < 20) kup_farmera(); if (farmerzy.size() == 20 && tanki.size()<25) kup_tanka(); } FOR(i,0,tanki.size()) { if (farmerzy.size() < 20) { int x = tanki[i].st, y = tanki[i].nd; if (A[x][y] < 0) continue; PII prz = znajdz_przeszkode(5,5); if (prz != mp(-1,-1)) { if (rand()%4 == 0) { idz_tanku_losowo(i); continue; } if (x < prz.st) rusz_tanka(i,x+1,y); else if (x > prz.st) rusz_tanka(i,x-1,y); else if (y > prz.nd) rusz_tanka(i,x,y-1); else if (y < prz.nd) rusz_tanka(i,x,y+1); continue; } if (x + y < 7) { idz_tanku_w_swiat(i); continue; } else if(idz_tanku_na_ujemnego_sasiada(i)) { continue; } else { idz_tanku_losowo(i); continue; } } else { int x = tanki[i].st, y = tanki[i].nd; if (A[x][y] < 0) continue; PII prz = znajdz_przeszkode(20,20); if (prz != mp(-1,-1)) { if (x < prz.st) { rusz_tanka(i,x+1,y); continue;} else if (x > prz.st) {rusz_tanka(i,x-1,y); continue;} else if (y > prz.nd) {rusz_tanka(i,x,y-1); continue;} else if (y < prz.nd) {rusz_tanka(i,x,y+1); continue;} continue; } if (x + y < 7) { idz_tanku_w_swiat(i); continue; } else if(idz_tanku_na_ujemnego_sasiada(i)) { continue; } else { idz_tanku_losowo(i); continue; } } } int FARMEROW = farmerzy.size(); FOR(i,0,farmerzy.size()) { if (farmerzy.size() < 5) { if (rand()%7 == 0) { losuj_farmerze(i); continue; } if (farmerzy[i].nd >= 100) { if (wracaj_farmerze(i)) { continue; } else { losuj_farmerze(i); continue; } } else { if (ruchy_poza[i] < 6) { idz_w_swiat(i); continue; } else if (rand()%2) { wybierz_max_z_dostepnych(i); continue; } else { wracaj_farmerze(i); continue; } } } else if (farmerzy.size() < 20) { if (rand()%6 == 0 && farmerzy[i].st.st + farmerzy[i].st.nd < 22) { idz_w_swiat(i); continue; } else if (rand()%6 == 0) { losuj_farmerze(i); continue; } int BND = 100; int W_SWIAT = 15; if (farmerzy[i].nd >= 200) { if (wracaj_farmerze(i)) { continue; } else { losuj_farmerze(i); continue; } } else { if (ruchy_poza[i] < W_SWIAT) { idz_w_swiat(i); continue; } else if (rand()%3) { wybierz_max_z_dostepnych(i); continue; } else { wracaj_farmerze(i); continue; } } } else { int x = farmerzy[i].st.st; int y = farmerzy[i].st.nd; if (A[x][y] > 0) continue; if (MOJE[i] != mp(-1,-1) && A[MOJE[i].st][MOJE[i].nd] == 0) { MOJE[i] = mp(-1,-1); } if (MOJE[i] == mp(-1,-1)) { MOJE[i] = znajdz_zloto(); } if (MOJE[i] != mp(-1,-1)) { if (rand()%5 == 0) { losuj_farmerze(i); continue; } else { PII zloto = MOJE[i]; if (zloto.st < x) { rusz_farmera(i, x-1, y); continue; } else if (zloto.st > x) { rusz_farmera(i, x+1, y); continue; } else if (zloto.nd > y) { rusz_farmera(i, x, y+1); continue; } else if (zloto.nd < y) { rusz_farmera(i, x, y-1); continue; } else { losuj_farmerze(i); continue; } } } else { if (farmerzy[i].nd > 0) { if (rand()%4) wracaj_farmerze(i); else losuj_farmerze(i); } else { if (x+y<7) idz_w_swiat(i); else losuj_farmerze(i); } } } } aktualizuj(); } int graj() { zeruj_zmienne(); wczytaj(); int licz = 0; if (!sa_zle) { while (1) { tura_bez_tankow(licz); licz++; printf("=\n"); if (sprawdz()) { printf("===\n"); break; } } return licz; } else { while (1) { tura_z_tankami(licz); licz++; printf("=\n"); if (sprawdz()) { printf("===\n"); break; } } } return licz; } int main() { srand(time(NULL)); read2(T, k); int res = 0; int suma = 0; FOR(i,0,T) { suma += graj(); } } |