#include <queue> #include <cstdlib> #include <tuple> #include <vector> #include <stdio.h> #include <iostream> #include <algorithm> #include <functional> using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair<int,int>; using pll = pair<ll,ll>; using vi = vector<int>; using vb = vector<bool>; using vc = vector<char>; using vvi = vector<vi>; using vvb = vector<vb>; using vpii = vector<pii>; using vpll = vector<pll>; using vll = vector<ll>; using vull = vector<ull>; #define ever (;;) #define f first #define s second #define pb emplace_back #define sz size() #define graph vector<vertex> #define deg(X) G[X].e.size() #define INF 1234567890 #define INFll 2000000000000000000 #define maxe(X, Y) if((Y) > (X)) (X) = (Y) #define mine(X, Y) if((Y) < (X)) (X) = (Y) #define rep(i, begin, end) for(__typeof(end) i = (begin); i < (end); ++i) #define repr(i, begin, end) for(__typeof(end) i = (begin)-1; i >= (end); --i) #define bend(X) X.begin(), X.end() #define D(X) dist[X.f][X.s] pii operator+ (const pii& A, const pii& B){ return {A.f + B.f, A.s + B.s}; } pii operator- (const pii& A) { return {-A.f, -A.s}; } int n; vvi T, dist; vvb occ; vvb czyMogeDojsc; int GOLD; int goldLeft, startGold, goldNotGiven; int goldAvailable; int stoneTiles; int farmerLimit, farmerCount; int tankLimit, tankCount; int dodatniePola; int timelimit; const vpii M = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; inline bool czy(const pii& V) { return V.f >= 0 && V.f < n && V.s >= 0 && V.s < n; } pii bfs(pii S, bool war(pii), bool kt(pii,pii)) { rep(i, 0, n) rep(j, 0, n) dist[i][j] = -10; queue<pii> Q; Q.emplace(S.f, S.s); D(S) = 1; pii R = S; while(!Q.empty()) { pii V = Q.front(); Q.pop(); if(kt(R, V)) R = V; for(pii i : M) { pii U = V+i; if(czy(U) && dist[U.f][U.s]<0 && war(U)) { D(U) = D(V)+1; Q.emplace(U); } } } return R; } pii dijkstra(pii S, bool war(pii), bool kt(pii, pii)) { rep(i, 0, n) rep(j, 0, n) dist[i][j] = -10; priority_queue<pair<int,pii>> Q; Q.emplace(-1, S); D(S) = 1; pii R = S; while(!Q.empty()) { pii V = Q.top().s; Q.pop(); if(kt(R, V)) R = V; for(pii i : M) { pii U = V+i; if(czy(U) && dist[U.f][U.s]<0 && war(U)) { D(U) = D(V)+1; if(T[U.f][U.s] < 0) D(U) += (-T[U.f][U.s]+1)/10; Q.emplace(-D(U), U); } } } return R; } vpii getPath(pii A, pii B) { vpii R; if(D(A) != 1 || D(B) < 0) return R; while(B != A) { for(pii i : M) { pii C = B+i; if(czy(C) && (D(C)+1 == D(B))) { R.pb(-i); B = C; break; } } } reverse(bend(R)); return R; } vpii getPathDijkstra(pii A, pii B) { vpii R; if(D(A) != 1 || D(B) < 0) return R; while(B != A) { for(pii i : M) { pii C = B+i; if(czy(C) && ( (T[B.f][B.s] <= 0 && D(B) == D(C)+1+(-T[B.f][B.s]+1)/10) || (T[B.f][B.s] >= 0 && D(B) == D(C)+1))) { R.pb(-i); B = C; break; } } } reverse(bend(R)); return R; } struct unit { public: unit() : pos{0, 0} { if(occ[0][0]) printf("occ[0][0] = 1; Error\n"); occ[0][0] = 1; }; pii pos; bool move(pii m) { pii V = pos+m; if(!czy(V) || occ[V.f][V.s]) return false; printf("M %d %d %d %d\n", pos.f, pos.s, V.f, V.s); occ[pos.f][pos.s] = 0; occ[V.f][V.s] = 1; pos = V; return true; }; }; struct farmer : public unit { farmer() : unit(), gold{0}, anger{0} { printf("R FARMER\n"); }; ull gold; vpii tasks; int anger; void end() { int k = min(T[pos.f][pos.s], 10); T[pos.f][pos.s] -= k; gold += k; goldLeft -= k; if(pos == pii{0, 0}) GOLD += gold, goldNotGiven -= gold, gold = 0; } void goTo(pii D) { vpii R = getPath(pos, D); reverse(bend(R)); tasks = R; } void getTasks() { pii D = pos; if(goldLeft > 0) { if(farmerCount < farmerLimit) T[0][0] = (gold/100)*(startGold/200); D = bfs(pos, [](pii V){return T[V.f][V.s] >= 0 && !occ[V.f][V.s];}, [](pii A, pii B) {return T[A.f][A.s]*D(B) < T[B.f][B.s]*D(A);} ); T[0][0] = 0; } else if(gold > 0) { rep(i, 0, n) T[0][i] = 2*n-i; rep(i, 1, n) T[1][i] = i; D = bfs(pos, [](pii V){return T[V.f][V.s] >= 0 && !occ[V.f][V.s];}, [](pii A, pii B) {return T[A.f][A.s] < T[B.f][B.s];} ); rep(i, 0, n) T[0][i] = 0, T[1][i] = 0; } else { D = bfs(pos, [](pii V){return T[V.f][V.s] == 0 && !occ[V.f][V.s];}, [](pii A, pii B)->bool {return T[A.f][A.s] > 0 || A.s%2==0 || A.f >= n || (T[B.f][B.s] == 0 && (A.f*2+A.s < B.f*2+B.s) && B.s%2 && B.f < n-1);} ); } goTo(D); } bool autopilot() { if(T[pos.f][pos.s] < 0) return 1; if(tasks.empty()) getTasks(); if(!tasks.empty()) { pii O = pos+tasks.back(); if(T[O.f][O.s] >= 0 && this->move(tasks.back())) { anger -= min(anger, 4); tasks.pop_back(); return 1; } else { ++anger; if(anger > 3) { tasks.clear(); getTasks(); } } } return 0; } }; struct tank : public unit { tank() : anger{0} { printf("R TANK\n"); }; vpii tasks; int anger; void end() { int k = min(0, max(T[pos.f][pos.s], -10)); T[pos.f][pos.s] -= k; if(k != 0 && T[pos.f][pos.s] == 0) --stoneTiles; } void goTo(pii D) { vpii R = getPathDijkstra(pos, D); reverse(bend(R)); tasks = R; } void getTasks() { pii D = pos; if(stoneTiles == 0) D = bfs(pos, [](pii V){return T[V.f][V.s] <= 0 && !occ[V.f][V.s];}, [](pii A, pii B)->bool {return T[A.f][A.s] > 0 || A.s%2==0 || A.f >= n || (T[B.f][B.s] == 0 && (A.f*2+A.s < B.f*2+B.s) && B.s%2 && B.f < n-1);} ); else { D = dijkstra(pos, [](pii V){return !occ[V.f][V.s];}, [](pii A, pii B) {return !czyMogeDojsc[B.f][B.s] && T[B.f][B.s] >= 0 && T[B.f][B.s]*D(A) > T[A.f][A.s]*D(B);} ); if(D == pos) D = dijkstra(pos, [](pii V){return !occ[V.f][V.s];}, [](pii A, pii B) {return (T[B.f][B.s]<0&&T[B.f][B.s]>T[A.f][A.s])||T[A.f][A.s]>=0;} ); } goTo(D); } bool autopilot() { if(tasks.empty()) getTasks(); if(!tasks.empty()) { if(this->move(tasks.back())) { anger -= min(anger, 4); tasks.pop_back(); return 1; } else { ++anger; if(anger > 3) { tasks.clear(); getTasks(); } } } } }; vector<farmer*> F; vector<tank*> TK; void in() { scanf("%d", &n); GOLD = 0; goldLeft = 0; startGold = 0; goldNotGiven = 0; goldAvailable = 0; stoneTiles = 0; farmerLimit = 0; farmerCount = 0; tankLimit = 0; tankCount = 0; dodatniePola = 0; T.clear(); T.resize(n, vi(n)); dist.clear(); dist.resize(n, vi(n, -10)); occ.clear(); occ.resize(n, vb(n, 0)); czyMogeDojsc.clear(); czyMogeDojsc.resize(n, vb(n, 0)); F.clear(); TK.clear(); rep(i, 0, n) rep(j, 0, n) { scanf("%d", &T[i][j]); if(T[i][j] > 0) goldLeft += T[i][j], ++dodatniePola; else if(T[i][j] < 0) ++stoneTiles; } } void getGoldAvailable() { bfs(pii{0, 0}, [](pii A)->bool{return T[A.f][A.s] >= 0;}, [](pii A, pii B)->bool{czyMogeDojsc[B.f][B.s] = 1; return false;} ); } int NO; void solve() { getGoldAvailable(); rep(i, 0, n) rep(j, 0, n) goldAvailable += (T[i][j]>0 && czyMogeDojsc[i][j])*T[i][j]; startGold = goldLeft; goldNotGiven = startGold; farmerLimit = 1+min({startGold/70, 4*n-1, dodatniePola/7}); tankLimit = min(stoneTiles*(2), n); if(startGold == goldAvailable) tankLimit = 0; GOLD += 200; if(T[1][0] < 0 && T[0][1] < 0) { TK.pb(new tank); ++tankCount; GOLD -= 100; } else { F.pb(new farmer); ++farmerCount; GOLD -= 100; } while(goldNotGiven > 0) { if(GOLD >= 100 && !occ[0][0]) { getGoldAvailable(); int Xfarmer = (farmerCount < farmerLimit)*(goldAvailable); int Xczolg = (tankCount < tankLimit)*(stoneTiles*80); if(farmerCount == 0) Xfarmer = 1, Xczolg = 0; if(Xfarmer > 0 && Xfarmer >= Xczolg && farmerCount < farmerLimit) { F.pb(new farmer); ++farmerCount; GOLD -= 100; } else if(Xczolg > 0 && Xczolg > Xfarmer && tankCount < tankLimit) { TK.pb(new tank); ++tankCount; GOLD -= 100; } } for(auto i : F) { i->autopilot(); i->end(); } for(auto i : TK) { i->autopilot(); i->end(); } printf("=\n"); ++NO; } printf("===\n"); } int main() { int tests; scanf("%d %d", &tests, &timelimit); rep(test, 0, tests) { in(); solve(); } //printf("Sredni czas: %d\n", NO/tests); 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 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 | #include <queue> #include <cstdlib> #include <tuple> #include <vector> #include <stdio.h> #include <iostream> #include <algorithm> #include <functional> using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair<int,int>; using pll = pair<ll,ll>; using vi = vector<int>; using vb = vector<bool>; using vc = vector<char>; using vvi = vector<vi>; using vvb = vector<vb>; using vpii = vector<pii>; using vpll = vector<pll>; using vll = vector<ll>; using vull = vector<ull>; #define ever (;;) #define f first #define s second #define pb emplace_back #define sz size() #define graph vector<vertex> #define deg(X) G[X].e.size() #define INF 1234567890 #define INFll 2000000000000000000 #define maxe(X, Y) if((Y) > (X)) (X) = (Y) #define mine(X, Y) if((Y) < (X)) (X) = (Y) #define rep(i, begin, end) for(__typeof(end) i = (begin); i < (end); ++i) #define repr(i, begin, end) for(__typeof(end) i = (begin)-1; i >= (end); --i) #define bend(X) X.begin(), X.end() #define D(X) dist[X.f][X.s] pii operator+ (const pii& A, const pii& B){ return {A.f + B.f, A.s + B.s}; } pii operator- (const pii& A) { return {-A.f, -A.s}; } int n; vvi T, dist; vvb occ; vvb czyMogeDojsc; int GOLD; int goldLeft, startGold, goldNotGiven; int goldAvailable; int stoneTiles; int farmerLimit, farmerCount; int tankLimit, tankCount; int dodatniePola; int timelimit; const vpii M = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; inline bool czy(const pii& V) { return V.f >= 0 && V.f < n && V.s >= 0 && V.s < n; } pii bfs(pii S, bool war(pii), bool kt(pii,pii)) { rep(i, 0, n) rep(j, 0, n) dist[i][j] = -10; queue<pii> Q; Q.emplace(S.f, S.s); D(S) = 1; pii R = S; while(!Q.empty()) { pii V = Q.front(); Q.pop(); if(kt(R, V)) R = V; for(pii i : M) { pii U = V+i; if(czy(U) && dist[U.f][U.s]<0 && war(U)) { D(U) = D(V)+1; Q.emplace(U); } } } return R; } pii dijkstra(pii S, bool war(pii), bool kt(pii, pii)) { rep(i, 0, n) rep(j, 0, n) dist[i][j] = -10; priority_queue<pair<int,pii>> Q; Q.emplace(-1, S); D(S) = 1; pii R = S; while(!Q.empty()) { pii V = Q.top().s; Q.pop(); if(kt(R, V)) R = V; for(pii i : M) { pii U = V+i; if(czy(U) && dist[U.f][U.s]<0 && war(U)) { D(U) = D(V)+1; if(T[U.f][U.s] < 0) D(U) += (-T[U.f][U.s]+1)/10; Q.emplace(-D(U), U); } } } return R; } vpii getPath(pii A, pii B) { vpii R; if(D(A) != 1 || D(B) < 0) return R; while(B != A) { for(pii i : M) { pii C = B+i; if(czy(C) && (D(C)+1 == D(B))) { R.pb(-i); B = C; break; } } } reverse(bend(R)); return R; } vpii getPathDijkstra(pii A, pii B) { vpii R; if(D(A) != 1 || D(B) < 0) return R; while(B != A) { for(pii i : M) { pii C = B+i; if(czy(C) && ( (T[B.f][B.s] <= 0 && D(B) == D(C)+1+(-T[B.f][B.s]+1)/10) || (T[B.f][B.s] >= 0 && D(B) == D(C)+1))) { R.pb(-i); B = C; break; } } } reverse(bend(R)); return R; } struct unit { public: unit() : pos{0, 0} { if(occ[0][0]) printf("occ[0][0] = 1; Error\n"); occ[0][0] = 1; }; pii pos; bool move(pii m) { pii V = pos+m; if(!czy(V) || occ[V.f][V.s]) return false; printf("M %d %d %d %d\n", pos.f, pos.s, V.f, V.s); occ[pos.f][pos.s] = 0; occ[V.f][V.s] = 1; pos = V; return true; }; }; struct farmer : public unit { farmer() : unit(), gold{0}, anger{0} { printf("R FARMER\n"); }; ull gold; vpii tasks; int anger; void end() { int k = min(T[pos.f][pos.s], 10); T[pos.f][pos.s] -= k; gold += k; goldLeft -= k; if(pos == pii{0, 0}) GOLD += gold, goldNotGiven -= gold, gold = 0; } void goTo(pii D) { vpii R = getPath(pos, D); reverse(bend(R)); tasks = R; } void getTasks() { pii D = pos; if(goldLeft > 0) { if(farmerCount < farmerLimit) T[0][0] = (gold/100)*(startGold/200); D = bfs(pos, [](pii V){return T[V.f][V.s] >= 0 && !occ[V.f][V.s];}, [](pii A, pii B) {return T[A.f][A.s]*D(B) < T[B.f][B.s]*D(A);} ); T[0][0] = 0; } else if(gold > 0) { rep(i, 0, n) T[0][i] = 2*n-i; rep(i, 1, n) T[1][i] = i; D = bfs(pos, [](pii V){return T[V.f][V.s] >= 0 && !occ[V.f][V.s];}, [](pii A, pii B) {return T[A.f][A.s] < T[B.f][B.s];} ); rep(i, 0, n) T[0][i] = 0, T[1][i] = 0; } else { D = bfs(pos, [](pii V){return T[V.f][V.s] == 0 && !occ[V.f][V.s];}, [](pii A, pii B)->bool {return T[A.f][A.s] > 0 || A.s%2==0 || A.f >= n || (T[B.f][B.s] == 0 && (A.f*2+A.s < B.f*2+B.s) && B.s%2 && B.f < n-1);} ); } goTo(D); } bool autopilot() { if(T[pos.f][pos.s] < 0) return 1; if(tasks.empty()) getTasks(); if(!tasks.empty()) { pii O = pos+tasks.back(); if(T[O.f][O.s] >= 0 && this->move(tasks.back())) { anger -= min(anger, 4); tasks.pop_back(); return 1; } else { ++anger; if(anger > 3) { tasks.clear(); getTasks(); } } } return 0; } }; struct tank : public unit { tank() : anger{0} { printf("R TANK\n"); }; vpii tasks; int anger; void end() { int k = min(0, max(T[pos.f][pos.s], -10)); T[pos.f][pos.s] -= k; if(k != 0 && T[pos.f][pos.s] == 0) --stoneTiles; } void goTo(pii D) { vpii R = getPathDijkstra(pos, D); reverse(bend(R)); tasks = R; } void getTasks() { pii D = pos; if(stoneTiles == 0) D = bfs(pos, [](pii V){return T[V.f][V.s] <= 0 && !occ[V.f][V.s];}, [](pii A, pii B)->bool {return T[A.f][A.s] > 0 || A.s%2==0 || A.f >= n || (T[B.f][B.s] == 0 && (A.f*2+A.s < B.f*2+B.s) && B.s%2 && B.f < n-1);} ); else { D = dijkstra(pos, [](pii V){return !occ[V.f][V.s];}, [](pii A, pii B) {return !czyMogeDojsc[B.f][B.s] && T[B.f][B.s] >= 0 && T[B.f][B.s]*D(A) > T[A.f][A.s]*D(B);} ); if(D == pos) D = dijkstra(pos, [](pii V){return !occ[V.f][V.s];}, [](pii A, pii B) {return (T[B.f][B.s]<0&&T[B.f][B.s]>T[A.f][A.s])||T[A.f][A.s]>=0;} ); } goTo(D); } bool autopilot() { if(tasks.empty()) getTasks(); if(!tasks.empty()) { if(this->move(tasks.back())) { anger -= min(anger, 4); tasks.pop_back(); return 1; } else { ++anger; if(anger > 3) { tasks.clear(); getTasks(); } } } } }; vector<farmer*> F; vector<tank*> TK; void in() { scanf("%d", &n); GOLD = 0; goldLeft = 0; startGold = 0; goldNotGiven = 0; goldAvailable = 0; stoneTiles = 0; farmerLimit = 0; farmerCount = 0; tankLimit = 0; tankCount = 0; dodatniePola = 0; T.clear(); T.resize(n, vi(n)); dist.clear(); dist.resize(n, vi(n, -10)); occ.clear(); occ.resize(n, vb(n, 0)); czyMogeDojsc.clear(); czyMogeDojsc.resize(n, vb(n, 0)); F.clear(); TK.clear(); rep(i, 0, n) rep(j, 0, n) { scanf("%d", &T[i][j]); if(T[i][j] > 0) goldLeft += T[i][j], ++dodatniePola; else if(T[i][j] < 0) ++stoneTiles; } } void getGoldAvailable() { bfs(pii{0, 0}, [](pii A)->bool{return T[A.f][A.s] >= 0;}, [](pii A, pii B)->bool{czyMogeDojsc[B.f][B.s] = 1; return false;} ); } int NO; void solve() { getGoldAvailable(); rep(i, 0, n) rep(j, 0, n) goldAvailable += (T[i][j]>0 && czyMogeDojsc[i][j])*T[i][j]; startGold = goldLeft; goldNotGiven = startGold; farmerLimit = 1+min({startGold/70, 4*n-1, dodatniePola/7}); tankLimit = min(stoneTiles*(2), n); if(startGold == goldAvailable) tankLimit = 0; GOLD += 200; if(T[1][0] < 0 && T[0][1] < 0) { TK.pb(new tank); ++tankCount; GOLD -= 100; } else { F.pb(new farmer); ++farmerCount; GOLD -= 100; } while(goldNotGiven > 0) { if(GOLD >= 100 && !occ[0][0]) { getGoldAvailable(); int Xfarmer = (farmerCount < farmerLimit)*(goldAvailable); int Xczolg = (tankCount < tankLimit)*(stoneTiles*80); if(farmerCount == 0) Xfarmer = 1, Xczolg = 0; if(Xfarmer > 0 && Xfarmer >= Xczolg && farmerCount < farmerLimit) { F.pb(new farmer); ++farmerCount; GOLD -= 100; } else if(Xczolg > 0 && Xczolg > Xfarmer && tankCount < tankLimit) { TK.pb(new tank); ++tankCount; GOLD -= 100; } } for(auto i : F) { i->autopilot(); i->end(); } for(auto i : TK) { i->autopilot(); i->end(); } printf("=\n"); ++NO; } printf("===\n"); } int main() { int tests; scanf("%d %d", &tests, &timelimit); rep(test, 0, tests) { in(); solve(); } //printf("Sredni czas: %d\n", NO/tests); return 0; } |