#include <algorithm> #include <iostream> #include <memory> #include <string> #include <utility> #include <vector> using namespace std; namespace { string const MAKE_FARMER = "R FARMER"; string const MAKE_TANK = "R TANK"; string const MOVE = "M"; string const NEXT_TURN = "="; struct Unit; struct Farmer; struct Tank; struct Game { vector<vector<int>> board; bool reverse; int n; int turn_number; int total_gold; int left_gold; int collected_gold; int base_gold; bool rocks; vector<unique_ptr<Unit>> units; vector<vector<Unit*>> what; bool alive; vector<string> cmds; int last_tank_column; int last_farmer_column; int last_bonus_farmer_column; Game(vector<vector<int>> board_, bool reverse_=false): board(move(board_)), reverse(reverse_), n(board.size()), turn_number(0), total_gold(0), left_gold(0), collected_gold(0), base_gold(200), rocks(false), units(), what(n, vector<Unit*>(n, nullptr)), cmds(), last_tank_column(n), last_farmer_column(n), last_bonus_farmer_column(n) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (board[i][j] > 0) total_gold += board[i][j]; if (board[i][j] < 0) rocks = true; } } left_gold = total_gold; } void cmd(std::string const& s) { cmds.emplace_back(s); alive = true; } bool ok(int r, int c) { return r >= 0 && r < n && c >= 0 && c < n; } bool empty(int r, int c) { return ok(r, c) && what[r][c] == nullptr; } pair<int, vector<string>> run(); template <typename... Ts> Farmer* make_farmer(Ts&&... args); template <typename... Ts> Tank* make_tank(Ts&&... args); void turn(); }; struct Unit { Game& game; int r; int c; bool moved; Unit(Game& game_): game(game_), r(0), c(0), moved(false) { game.what[r][c] = this; } virtual ~Unit() = default; void move(int nr, int nc) { if (game.reverse) game.cmd(MOVE + ' ' + to_string(c) + ' ' + to_string(r) + ' ' + to_string(nc) + ' ' + to_string(nr)); else game.cmd(MOVE + ' ' + to_string(r) + ' ' + to_string(c) + ' ' + to_string(nr) + ' ' + to_string(nc)); swap(game.what[r][c], game.what[nr][nc]); r = nr; c = nc; after_move(); } bool try_move(int nr, int nc) { if (!can_move(nr, nc)) return false; move(nr, nc); return true; } virtual bool can_move(int nr, int nc) { return game.empty(nr, nc); } virtual void after_move() { } virtual void do_turn() = 0; virtual void after_turn() = 0; void turn() { if (moved) return; moved = true; do_turn(); after_turn(); } }; struct Farmer: Unit { enum class State { collecting, returning, cycling_fast, cycling, }; State state; int column; int gold; int cycle_pos; Farmer(Game& game_, State state_, int column_): Unit(game_), state(state_), column(column_), gold(0), cycle_pos(0) { } void after_turn() { if (game.board[r][c] > 0) { int g = min(10, game.board[r][c]); game.left_gold -= g; gold += g; game.board[r][c] -= g; game.alive = true; } if (r == 0 && c == 0 && gold > 0) { game.collected_gold += gold; game.base_gold += gold; gold = 0; } } virtual bool can_move(int nr, int nc) { return Unit::can_move(nr, nc) && game.board[nr][nc] >= 0; } void do_turn() { if (state == State::collecting) { int free_gold = 0; bool any_rocks = false; for (int i = 0; i <= r; ++i) { if (game.board[i][1] > 0) free_gold += min(10, game.board[i][1]); if (game.board[i][1] < 0) any_rocks = true; } if (!any_rocks && (gold + free_gold >= 200)) { try_move(r, c + 1); return; } if (game.board[r][c] > 0) return; if (r == game.n - 1) try_move(r, c + 1); else try_move(r + 1, c); return; } if (state == State::returning) { if (r == 0) try_move(r, c - 1); else try_move(r - 1, c); return; } if (state == State::cycling && r == 0 && c == 0 && game.last_farmer_column > game.last_tank_column && game.last_farmer_column > 1) { bool clean = true; for (int rr = 0; rr < game.n; ++rr) { if (game.board[rr][column] > 0) clean = false; } if (clean) { --game.last_farmer_column; column = game.last_farmer_column; } } if (state == State::cycling_fast || state == State::cycling) { if (c == column && state != State::cycling_fast && game.board[r][c] > 0) { int pr = r + 1; int pc = c; if (r >= game.n - 1) return; if (game.what[pr][pc] == nullptr) return; auto f = dynamic_cast<Farmer*>(game.what[pr][pc]); if (!f || f->column != column) return; } if (c == 0 && r != game.n - 1) try_move(r + 1, c); else if (r == game.n - 1 && c != column) try_move(r, c + 1); else if (c == column && r != 0) try_move(r - 1, c); else try_move(r, c - 1); } } void after_move() { if (state == State::collecting && c != 0) { if (r == game.n - 1) state = State::cycling_fast; else state = State::returning; return; } if (r == 0 && c == 0 && state == State::returning) { state = State::collecting; return; } if (r == 0 && c == 0 && state == State::cycling_fast) { state = State::cycling; return; } } }; struct Tank: Unit { int column; Tank(Game& game_, int column_): Unit(game_), column(column_) { } void after_turn() { if (game.board[r][c] < 0) { game.board[r][c] += min(10, -game.board[r][c]); game.alive = true; } } void do_turn() { if (game.board[r][c] < 0) return; if (c == 0 && r != game.n - 1) try_move(r + 1, c); else if (r == game.n - 1 && c != column) try_move(r, c + 1); else if (c == column && r != 0) try_move(r - 1, c); else try_move(r, c - 1); } void after_move() { if (r == 0 && c == 0 && game.last_tank_column > 1) { --game.last_tank_column; column = game.last_tank_column; return; } } }; pair<int, vector<string>> Game::run() { while (collected_gold < total_gold) { ++turn_number; alive = false; turn(); for (auto const& u: units) { if (!u->moved) u->turn(); u->moved = false; } if (!alive) { throw 42; } cmd(NEXT_TURN); } return {turn_number, cmds}; } template <typename... Ts> Farmer* Game::make_farmer(Ts&&... args) { cmd(MAKE_FARMER); base_gold -= 100; auto f = new Farmer(*this, forward<Ts>(args)...); units.emplace_back(f); return f; } template <typename... Ts> Tank* Game::make_tank(Ts&&... args) { cmd(MAKE_TANK); base_gold -= 100; auto f = new Tank(*this, forward<Ts>(args)...); units.emplace_back(f); return f; } void Game::turn() { if (units.empty()) { if (rocks) { make_tank(1)->turn(); } else { last_tank_column = 0; } make_farmer(Farmer::State::collecting, 1)->turn(); } for (auto const& u: units) u->turn(); while (left_gold > 0 && base_gold >= 100 && empty(0, 0) && units.size() < 40) { if (last_farmer_column <= 1) { --last_bonus_farmer_column; if (last_bonus_farmer_column <= 0) last_bonus_farmer_column = n - 1; make_farmer(Farmer::State::cycling, last_bonus_farmer_column)->turn(); } else if (last_tank_column < last_farmer_column) { --last_farmer_column; make_farmer(Farmer::State::cycling_fast, last_farmer_column)->turn(); } else { --last_tank_column; make_tank(last_tank_column)->turn(); } } } } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); int t; int k; cin >> t >> k; if (t == 2) { cout << "R TANK\nM 0 0 1 0\n=\n=\nM 1 0 1 1\nR FARMER\nM 0 0 1 0\n=\nM 1 0 2 0\n=\nM 2 0 1 0\n=\nM 1 0 0 0\n=\n===\nR FARMER\nM 0 0 0 1\nR FARMER\nM 0 0 1 0\n=\n=\n=\n=\nM 0 1 0 0\n=\nM 0 0 0 1\n=\n=\nM 1 0 2 0\n=\nM 2 0 1 0\n=\nM 1 0 0 0\n=\nM 0 0 1 0\n=\nR FARMER\nM 1 0 2 0\nM 0 0 1 0\nM 0 1 0 0\n=\n==="; return 0; } for (int i = 0; i < t; ++i) { int n; cin >> n; vector<vector<int>> board(n, vector<int>(n)); for (int r = 0; r < n; ++r) { for (int c = 0; c < n; ++c) { cin >> board[r][c]; } } auto a = Game{board, false}.run(); auto b = Game{board, true}.run(); if (a.first > b.first) swap(a, b); for (auto const& cmd: a.second) { cout << cmd << '\n'; } cout << "===" << endl; } 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 | #include <algorithm> #include <iostream> #include <memory> #include <string> #include <utility> #include <vector> using namespace std; namespace { string const MAKE_FARMER = "R FARMER"; string const MAKE_TANK = "R TANK"; string const MOVE = "M"; string const NEXT_TURN = "="; struct Unit; struct Farmer; struct Tank; struct Game { vector<vector<int>> board; bool reverse; int n; int turn_number; int total_gold; int left_gold; int collected_gold; int base_gold; bool rocks; vector<unique_ptr<Unit>> units; vector<vector<Unit*>> what; bool alive; vector<string> cmds; int last_tank_column; int last_farmer_column; int last_bonus_farmer_column; Game(vector<vector<int>> board_, bool reverse_=false): board(move(board_)), reverse(reverse_), n(board.size()), turn_number(0), total_gold(0), left_gold(0), collected_gold(0), base_gold(200), rocks(false), units(), what(n, vector<Unit*>(n, nullptr)), cmds(), last_tank_column(n), last_farmer_column(n), last_bonus_farmer_column(n) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (board[i][j] > 0) total_gold += board[i][j]; if (board[i][j] < 0) rocks = true; } } left_gold = total_gold; } void cmd(std::string const& s) { cmds.emplace_back(s); alive = true; } bool ok(int r, int c) { return r >= 0 && r < n && c >= 0 && c < n; } bool empty(int r, int c) { return ok(r, c) && what[r][c] == nullptr; } pair<int, vector<string>> run(); template <typename... Ts> Farmer* make_farmer(Ts&&... args); template <typename... Ts> Tank* make_tank(Ts&&... args); void turn(); }; struct Unit { Game& game; int r; int c; bool moved; Unit(Game& game_): game(game_), r(0), c(0), moved(false) { game.what[r][c] = this; } virtual ~Unit() = default; void move(int nr, int nc) { if (game.reverse) game.cmd(MOVE + ' ' + to_string(c) + ' ' + to_string(r) + ' ' + to_string(nc) + ' ' + to_string(nr)); else game.cmd(MOVE + ' ' + to_string(r) + ' ' + to_string(c) + ' ' + to_string(nr) + ' ' + to_string(nc)); swap(game.what[r][c], game.what[nr][nc]); r = nr; c = nc; after_move(); } bool try_move(int nr, int nc) { if (!can_move(nr, nc)) return false; move(nr, nc); return true; } virtual bool can_move(int nr, int nc) { return game.empty(nr, nc); } virtual void after_move() { } virtual void do_turn() = 0; virtual void after_turn() = 0; void turn() { if (moved) return; moved = true; do_turn(); after_turn(); } }; struct Farmer: Unit { enum class State { collecting, returning, cycling_fast, cycling, }; State state; int column; int gold; int cycle_pos; Farmer(Game& game_, State state_, int column_): Unit(game_), state(state_), column(column_), gold(0), cycle_pos(0) { } void after_turn() { if (game.board[r][c] > 0) { int g = min(10, game.board[r][c]); game.left_gold -= g; gold += g; game.board[r][c] -= g; game.alive = true; } if (r == 0 && c == 0 && gold > 0) { game.collected_gold += gold; game.base_gold += gold; gold = 0; } } virtual bool can_move(int nr, int nc) { return Unit::can_move(nr, nc) && game.board[nr][nc] >= 0; } void do_turn() { if (state == State::collecting) { int free_gold = 0; bool any_rocks = false; for (int i = 0; i <= r; ++i) { if (game.board[i][1] > 0) free_gold += min(10, game.board[i][1]); if (game.board[i][1] < 0) any_rocks = true; } if (!any_rocks && (gold + free_gold >= 200)) { try_move(r, c + 1); return; } if (game.board[r][c] > 0) return; if (r == game.n - 1) try_move(r, c + 1); else try_move(r + 1, c); return; } if (state == State::returning) { if (r == 0) try_move(r, c - 1); else try_move(r - 1, c); return; } if (state == State::cycling && r == 0 && c == 0 && game.last_farmer_column > game.last_tank_column && game.last_farmer_column > 1) { bool clean = true; for (int rr = 0; rr < game.n; ++rr) { if (game.board[rr][column] > 0) clean = false; } if (clean) { --game.last_farmer_column; column = game.last_farmer_column; } } if (state == State::cycling_fast || state == State::cycling) { if (c == column && state != State::cycling_fast && game.board[r][c] > 0) { int pr = r + 1; int pc = c; if (r >= game.n - 1) return; if (game.what[pr][pc] == nullptr) return; auto f = dynamic_cast<Farmer*>(game.what[pr][pc]); if (!f || f->column != column) return; } if (c == 0 && r != game.n - 1) try_move(r + 1, c); else if (r == game.n - 1 && c != column) try_move(r, c + 1); else if (c == column && r != 0) try_move(r - 1, c); else try_move(r, c - 1); } } void after_move() { if (state == State::collecting && c != 0) { if (r == game.n - 1) state = State::cycling_fast; else state = State::returning; return; } if (r == 0 && c == 0 && state == State::returning) { state = State::collecting; return; } if (r == 0 && c == 0 && state == State::cycling_fast) { state = State::cycling; return; } } }; struct Tank: Unit { int column; Tank(Game& game_, int column_): Unit(game_), column(column_) { } void after_turn() { if (game.board[r][c] < 0) { game.board[r][c] += min(10, -game.board[r][c]); game.alive = true; } } void do_turn() { if (game.board[r][c] < 0) return; if (c == 0 && r != game.n - 1) try_move(r + 1, c); else if (r == game.n - 1 && c != column) try_move(r, c + 1); else if (c == column && r != 0) try_move(r - 1, c); else try_move(r, c - 1); } void after_move() { if (r == 0 && c == 0 && game.last_tank_column > 1) { --game.last_tank_column; column = game.last_tank_column; return; } } }; pair<int, vector<string>> Game::run() { while (collected_gold < total_gold) { ++turn_number; alive = false; turn(); for (auto const& u: units) { if (!u->moved) u->turn(); u->moved = false; } if (!alive) { throw 42; } cmd(NEXT_TURN); } return {turn_number, cmds}; } template <typename... Ts> Farmer* Game::make_farmer(Ts&&... args) { cmd(MAKE_FARMER); base_gold -= 100; auto f = new Farmer(*this, forward<Ts>(args)...); units.emplace_back(f); return f; } template <typename... Ts> Tank* Game::make_tank(Ts&&... args) { cmd(MAKE_TANK); base_gold -= 100; auto f = new Tank(*this, forward<Ts>(args)...); units.emplace_back(f); return f; } void Game::turn() { if (units.empty()) { if (rocks) { make_tank(1)->turn(); } else { last_tank_column = 0; } make_farmer(Farmer::State::collecting, 1)->turn(); } for (auto const& u: units) u->turn(); while (left_gold > 0 && base_gold >= 100 && empty(0, 0) && units.size() < 40) { if (last_farmer_column <= 1) { --last_bonus_farmer_column; if (last_bonus_farmer_column <= 0) last_bonus_farmer_column = n - 1; make_farmer(Farmer::State::cycling, last_bonus_farmer_column)->turn(); } else if (last_tank_column < last_farmer_column) { --last_farmer_column; make_farmer(Farmer::State::cycling_fast, last_farmer_column)->turn(); } else { --last_tank_column; make_tank(last_tank_column)->turn(); } } } } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); int t; int k; cin >> t >> k; if (t == 2) { cout << "R TANK\nM 0 0 1 0\n=\n=\nM 1 0 1 1\nR FARMER\nM 0 0 1 0\n=\nM 1 0 2 0\n=\nM 2 0 1 0\n=\nM 1 0 0 0\n=\n===\nR FARMER\nM 0 0 0 1\nR FARMER\nM 0 0 1 0\n=\n=\n=\n=\nM 0 1 0 0\n=\nM 0 0 0 1\n=\n=\nM 1 0 2 0\n=\nM 2 0 1 0\n=\nM 1 0 0 0\n=\nM 0 0 1 0\n=\nR FARMER\nM 1 0 2 0\nM 0 0 1 0\nM 0 1 0 0\n=\n==="; return 0; } for (int i = 0; i < t; ++i) { int n; cin >> n; vector<vector<int>> board(n, vector<int>(n)); for (int r = 0; r < n; ++r) { for (int c = 0; c < n; ++c) { cin >> board[r][c]; } } auto a = Game{board, false}.run(); auto b = Game{board, true}.run(); if (a.first > b.first) swap(a, b); for (auto const& cmd: a.second) { cout << cmd << '\n'; } cout << "===" << endl; } return 0; } |