#include <iostream> #include <cstdio> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <algorithm> #include <cstring> #include <ctime> #include <cassert> using namespace std; using ll = long long; const int INF = 1e9; const bool DEBUG = true; void CMDbuyFarmer() { cout << "R FARMER" << endl; } void CMDmakeMove(int i1, int j1, int i2, int j2) { cout << "M " << i1 << " " << j1 << " " << i2 << " " << j2 << endl; } void CMDendRound() { cout << "=" << endl; } void CMDendGame() { cout << "===" << endl; } struct Farmer { int row; int col; void move(int newRow, int newCol) { CMDmakeMove(row, col, newRow, newCol); row = newRow; col = newCol; } void moveRight() { move(row, col+1); } void moveLeft() { move(row, col-1); } void moveUp() { move(row-1, col); } void moveDown() { move(row+1, col); } }; struct Game { deque<Farmer> farmers; int currentGold = 0; int allGold = 0; int takenGold = 0; int numRounds = 0; vector<vector<int>> a; int n; Game(const vector<vector<int>>& board) { a = board; n = board.size(); currentGold = 200; takenGold = 0; numRounds = 0; farmers.clear(); for (auto row : a) { for (auto x : row) { allGold += max(0, x); } } } void printBoard() { if (!DEBUG) return; cerr << "###" << endl; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { bool foundFarmer = false; for (auto& farmer : farmers) { if (i == farmer.row and j == farmer.col) { foundFarmer = true; break; } } if (foundFarmer) { cerr << "*"; } else { cerr << "."; } } cerr << endl; } cerr << "###" << endl; } void buyFarmer() { assert(currentGold >= 100); CMDbuyFarmer(); farmers.push_front({0, 0}); currentGold -= 100; } void check() { set<pair<int, int>> pos; for (auto f : farmers) { pos.insert({f.row, f.col}); } assert(pos.size() == farmers.size()); } void endRound() { for (auto& f : farmers) { takeGold(f); } ++numRounds; CMDendRound(); if (DEBUG) { printBoard(); } } void endGame() { CMDendGame(); } void moveFarmerUp(Farmer& f) { f.moveUp(); if (DEBUG) { check(); } } void moveFarmerDown(Farmer& f) { f.moveDown(); if (DEBUG) { check(); } } void moveFarmerLeft(Farmer& f) { f.moveLeft(); if (DEBUG) { check(); } } void moveFarmerRight(Farmer& f) { f.moveRight(); if (DEBUG) { check(); } } void moveSingleFarmerToAndEndRoundThere(Farmer& f, int row, int col) { while (f.row != row) { if (f.row < row) { moveFarmerDown(f); } else { moveFarmerUp(f); } endRound(); } while (f.col != col) { if (f.col < col) { moveFarmerRight(f); } else { moveFarmerLeft(f); } endRound(); } endRound(); } void takeGold(Farmer& f) { int taken = max(0, min(a[f.row][f.col], 10)); a[f.row][f.col] -= taken; takenGold += taken; currentGold += taken; } void playWithoutWalls() { buyFarmer(); vector<pair<int, pair<int, int>>> values; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { values.push_back({a[i][j], {i, j}}); } } sort(values.begin(), values.end()); while (currentGold < 1900) { // zbieramy zloto jakos jednym farmerem int row = values.back().second.first; int col = values.back().second.second; //cout << "ruszamy do " << row << " " << col << endl; if (a[row][col] == 0) { values.pop_back(); } else { moveSingleFarmerToAndEndRoundThere(farmers.front(), row, col); } } // wracamy farmerem do bazy moveSingleFarmerToAndEndRoundThere(farmers.front(), 0, 0); // przesuwamy farmerow i kupujemy nowych while (farmers.size() < n) { for (int i = (int)farmers.size()-1; i >= 0; --i) { moveFarmerRight(farmers[i]); } buyFarmer(); endRound(); } // chodz farmerami po swoich kolumnach az uzbieraja wszystko while (true) { if (takenGold == allGold) { break; } for (auto& f : farmers) { if (a[f.row][f.col] == 0 and f.row+1 < n) { moveFarmerDown(f); } else { takeGold(f); } } endRound(); } // przesun wszystkich farmerow do gory while (true) { bool allWereUp = true; for (auto& f : farmers) { if (f.row > 0) { allWereUp = false; moveFarmerUp(f); } } if (!allWereUp) { endRound(); } else { break; } } // dopoko nie wszyscy farmerzy sa w pierwszej kolumnie while (true) { bool anyNotInFirstCol = false; for (auto& f : farmers) { if (f.col > 0) { anyNotInFirstCol = true; } } if (!anyNotInFirstCol) { break; } // przesun wszystkich z pierwszej kolumny o jeden w dol for (auto& f : farmers) { if (f.col == 0) { moveFarmerDown(f); } } // przesun wszystkich z pierwszego wiersza o jeden w prawo for (auto& f : farmers) { if (f.row == 0) { moveFarmerLeft(f); } } endRound(); } return; endGame(); } void playWithWalls() { endGame(); } }; int main() { ios_base::sync_with_stdio(0); int t, k; cin >> t >> k; while (t--) { int n; cin >> n; bool seenWalls = 0; int gold = 0; vector<vector<int>> a(n, vector<int>(n)); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cin >> a[i][j]; if (a[i][j] < 0) { seenWalls = true; } else { gold += a[i][j]; } } } Game game(a); if (!seenWalls) { game.playWithoutWalls(); } else { game.playWithWalls(); } if (DEBUG) { cerr << "numRounds=" << game.numRounds << endl; cerr << "allGold=" << gold << endl; cerr << "takenGold=" << game.takenGold << 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 | #include <iostream> #include <cstdio> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <algorithm> #include <cstring> #include <ctime> #include <cassert> using namespace std; using ll = long long; const int INF = 1e9; const bool DEBUG = true; void CMDbuyFarmer() { cout << "R FARMER" << endl; } void CMDmakeMove(int i1, int j1, int i2, int j2) { cout << "M " << i1 << " " << j1 << " " << i2 << " " << j2 << endl; } void CMDendRound() { cout << "=" << endl; } void CMDendGame() { cout << "===" << endl; } struct Farmer { int row; int col; void move(int newRow, int newCol) { CMDmakeMove(row, col, newRow, newCol); row = newRow; col = newCol; } void moveRight() { move(row, col+1); } void moveLeft() { move(row, col-1); } void moveUp() { move(row-1, col); } void moveDown() { move(row+1, col); } }; struct Game { deque<Farmer> farmers; int currentGold = 0; int allGold = 0; int takenGold = 0; int numRounds = 0; vector<vector<int>> a; int n; Game(const vector<vector<int>>& board) { a = board; n = board.size(); currentGold = 200; takenGold = 0; numRounds = 0; farmers.clear(); for (auto row : a) { for (auto x : row) { allGold += max(0, x); } } } void printBoard() { if (!DEBUG) return; cerr << "###" << endl; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { bool foundFarmer = false; for (auto& farmer : farmers) { if (i == farmer.row and j == farmer.col) { foundFarmer = true; break; } } if (foundFarmer) { cerr << "*"; } else { cerr << "."; } } cerr << endl; } cerr << "###" << endl; } void buyFarmer() { assert(currentGold >= 100); CMDbuyFarmer(); farmers.push_front({0, 0}); currentGold -= 100; } void check() { set<pair<int, int>> pos; for (auto f : farmers) { pos.insert({f.row, f.col}); } assert(pos.size() == farmers.size()); } void endRound() { for (auto& f : farmers) { takeGold(f); } ++numRounds; CMDendRound(); if (DEBUG) { printBoard(); } } void endGame() { CMDendGame(); } void moveFarmerUp(Farmer& f) { f.moveUp(); if (DEBUG) { check(); } } void moveFarmerDown(Farmer& f) { f.moveDown(); if (DEBUG) { check(); } } void moveFarmerLeft(Farmer& f) { f.moveLeft(); if (DEBUG) { check(); } } void moveFarmerRight(Farmer& f) { f.moveRight(); if (DEBUG) { check(); } } void moveSingleFarmerToAndEndRoundThere(Farmer& f, int row, int col) { while (f.row != row) { if (f.row < row) { moveFarmerDown(f); } else { moveFarmerUp(f); } endRound(); } while (f.col != col) { if (f.col < col) { moveFarmerRight(f); } else { moveFarmerLeft(f); } endRound(); } endRound(); } void takeGold(Farmer& f) { int taken = max(0, min(a[f.row][f.col], 10)); a[f.row][f.col] -= taken; takenGold += taken; currentGold += taken; } void playWithoutWalls() { buyFarmer(); vector<pair<int, pair<int, int>>> values; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { values.push_back({a[i][j], {i, j}}); } } sort(values.begin(), values.end()); while (currentGold < 1900) { // zbieramy zloto jakos jednym farmerem int row = values.back().second.first; int col = values.back().second.second; //cout << "ruszamy do " << row << " " << col << endl; if (a[row][col] == 0) { values.pop_back(); } else { moveSingleFarmerToAndEndRoundThere(farmers.front(), row, col); } } // wracamy farmerem do bazy moveSingleFarmerToAndEndRoundThere(farmers.front(), 0, 0); // przesuwamy farmerow i kupujemy nowych while (farmers.size() < n) { for (int i = (int)farmers.size()-1; i >= 0; --i) { moveFarmerRight(farmers[i]); } buyFarmer(); endRound(); } // chodz farmerami po swoich kolumnach az uzbieraja wszystko while (true) { if (takenGold == allGold) { break; } for (auto& f : farmers) { if (a[f.row][f.col] == 0 and f.row+1 < n) { moveFarmerDown(f); } else { takeGold(f); } } endRound(); } // przesun wszystkich farmerow do gory while (true) { bool allWereUp = true; for (auto& f : farmers) { if (f.row > 0) { allWereUp = false; moveFarmerUp(f); } } if (!allWereUp) { endRound(); } else { break; } } // dopoko nie wszyscy farmerzy sa w pierwszej kolumnie while (true) { bool anyNotInFirstCol = false; for (auto& f : farmers) { if (f.col > 0) { anyNotInFirstCol = true; } } if (!anyNotInFirstCol) { break; } // przesun wszystkich z pierwszej kolumny o jeden w dol for (auto& f : farmers) { if (f.col == 0) { moveFarmerDown(f); } } // przesun wszystkich z pierwszego wiersza o jeden w prawo for (auto& f : farmers) { if (f.row == 0) { moveFarmerLeft(f); } } endRound(); } return; endGame(); } void playWithWalls() { endGame(); } }; int main() { ios_base::sync_with_stdio(0); int t, k; cin >> t >> k; while (t--) { int n; cin >> n; bool seenWalls = 0; int gold = 0; vector<vector<int>> a(n, vector<int>(n)); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cin >> a[i][j]; if (a[i][j] < 0) { seenWalls = true; } else { gold += a[i][j]; } } } Game game(a); if (!seenWalls) { game.playWithoutWalls(); } else { game.playWithWalls(); } if (DEBUG) { cerr << "numRounds=" << game.numRounds << endl; cerr << "allGold=" << gold << endl; cerr << "takenGold=" << game.takenGold << endl; } } return 0; } |