#include <iostream> #include <vector> #include <algorithm> #define REP(x,n) for(int x=0;x<(n);++x) using namespace std; int T,k,n,x; const int MAX_N = 20; int collectedGold; int goldInBase; int searchCounter=0; struct Cell; const int VISITED_FLAG = 0x010000; const int WORKER_FLAG = 0x020000; const int FARMER_FLAG = 0x040000 | WORKER_FLAG; const int TANK_FLAG = 0x080000 | WORKER_FLAG; const int RESERVED_FLAG =0x100000; const int ALL_FLAGS = 0x1F0000; const bool PRINT_MOVES=true; const bool DEBUG_MOVES=false; const bool DEBUG_FUNCTIONS=false; const bool COUNT_TURNS=true; int numberOfTurns; int cellsWithGold; int reservedCells; int unitCounter=0; struct Unit { Cell* cell; int gold; bool moved; Cell* targetCell; int id; Unit(Cell* cell): cell(cell), gold(0), moved(false), targetCell(nullptr), id(++unitCounter) { } }; struct Cell { int row; int col; int numberOfGold; int numberOfRocks; int flags; int searchId; int searchValue; int searchDistance; Cell* searchParent; vector<pair<Cell*,bool>> neighbours; Cell *leftNeighbour, *rightNeighbour, *topNeighbour, *bottomNeighbour; void set(int row, int col, int value, Cell board[MAX_N][MAX_N]) { leftNeighbour=rightNeighbour=topNeighbour=bottomNeighbour=nullptr; this->row = row; this->col = col; flags = 0; numberOfGold = max(0, value); numberOfRocks = max(0, -value); searchId = 0; neighbours.clear(); if(row>0) neighbours.push_back(make_pair(topNeighbour=&board[row-1][col], true)); if(col>0) neighbours.push_back(make_pair(leftNeighbour=&board[row][col-1], true)); if(row+1<n) neighbours.push_back(make_pair(bottomNeighbour=&board[row+1][col], false)); if(col+1<n) neighbours.push_back(make_pair(rightNeighbour=&board[row][col+1], false)); } int operator|(int value) const {return flags|value;} int operator&(int value) const {return flags&value;} int operator|=(int value) {return flags|=value;} int operator&=(int value) {return flags&=value;} bool isReserved() const {return flags&RESERVED_FLAG;} bool canEnter() const {return !(flags&WORKER_FLAG);}; }; ostream& operator<<(ostream& os, const Cell& cell) { int value = (cell.numberOfGold>0 ? cell.numberOfGold : -cell.numberOfRocks); string prefix = value>100 ? " " : value>10 ? " " : value>=0 ? " " : value>-10 ? " " : value>-100 ? " " : ""; os << prefix << value; if(cell.flags&TANK_FLAG) os << "T"; else if(cell.flags&FARMER_FLAG) os << "F"; else if(cell.flags&RESERVED_FLAG) os<<"R"; else os << " "; return os; } Cell board[MAX_N][MAX_N]; vector<Unit*> farmers, tanks; int inline sgn(int val) { return (val>0) - (val<0); } void moveTank(Unit& tank, Cell* target) { if(PRINT_MOVES) cout << "M "<<tank.cell->row<<" "<<tank.cell->col<<" "<<target->row<<" "<<target->col<<"\n"; (*tank.cell) &= ~TANK_FLAG; tank.cell=target; (*target) |= TANK_FLAG; tank.moved=true; } void moveFarmer(Unit& farmer, Cell* target) { if(DEBUG_FUNCTIONS) { cerr << "MOVE_FARMER " << farmer.id<<" -> "; if(target==nullptr) cerr << "[NULL]" << endl; else cerr << target->row<<","<<target->col<<endl; } if(PRINT_MOVES) cout << "M "<<farmer.cell->row<<" "<<farmer.cell->col<<" "<<target->row<<" "<<target->col<<"\n"; (*farmer.cell) &= ~FARMER_FLAG; farmer.cell=target; (*target) |= FARMER_FLAG; farmer.moved=true; if(DEBUG_FUNCTIONS) cerr << "/MOVE_FARMER" << endl; } void routeFarmer(Unit& farmer) { if(DEBUG_FUNCTIONS) cerr<<"ROUTE_FARMER "<<farmer.id<<" ["<<farmer.cell->row<<","<<farmer.cell->col<<"]"<<endl; if(farmer.moved) return; int searchNumber = ++searchCounter; farmer.cell->searchId = searchNumber; farmer.cell->searchValue = 0; farmer.cell->searchDistance = 0; vector<Cell*> v1 = {farmer.cell}, *current=&v1; vector<Cell*> v2, *future=&v2; int distance=0; for(bool stop=false;!stop && !current->empty();swap(current,future),++distance) { future->clear(); for(Cell* cell : *current) { for(pair<Cell*,bool>& neighbour : cell->neighbours) if(neighbour.first->canEnter()) { if(neighbour.first == farmer.targetCell) stop = true; if(neighbour.first->searchId!=searchNumber) { future->push_back(neighbour.first); neighbour.first->searchId = searchNumber; neighbour.first->searchDistance = cell->searchDistance+1; neighbour.first->searchValue = cell->searchValue + neighbour.first->numberOfGold; neighbour.first->searchParent = cell; } else if(neighbour.first->searchDistance>cell->searchDistance && neighbour.first->searchValue < cell->searchValue + neighbour.first->numberOfGold) { neighbour.first->searchValue = cell->searchValue + neighbour.first->numberOfGold; neighbour.first->searchParent = cell; } } } } Cell* nextCell = farmer.targetCell; if(current->empty() && distance==1) { if(DEBUG_MOVES) cerr << "path to "<<farmer.targetCell->row<<","<<farmer.targetCell->col<<" was not found"<<endl; return; } else if(current->empty()) { nextCell = future->back(); if(DEBUG_MOVES) cerr << "path to "<<farmer.targetCell->row<<","<<farmer.targetCell->col<<" was not found, going to closest reachable - "<<nextCell->row<<","<<nextCell->col<<endl; } for(;nextCell->searchParent != farmer.cell; nextCell = nextCell->searchParent); if(DEBUG_MOVES) cerr << "best path to "<<farmer.targetCell->row<<","<<farmer.targetCell->col<<" is via "<<nextCell->row<<","<<nextCell->col<<endl; moveFarmer(farmer, nextCell); if(farmer.cell == farmer.targetCell && (farmer.cell->flags & RESERVED_FLAG)) { farmer.cell->flags &= ~RESERVED_FLAG; --reservedCells; } if(DEBUG_FUNCTIONS) cerr<<"/ROUTE_FARMER "<<farmer.id<<endl; } void moveOutFarmer(Unit& farmer) { if(DEBUG_FUNCTIONS) cerr<<"===MOVE_OUT "<<farmer.id<<" ["<<farmer.cell->row<<","<<farmer.cell->col<<"]"<<endl; farmer.targetCell = &board[n-1][n-1]; routeFarmer(farmer); if(farmer.moved) return; for(int row=n-1;row>=0;--row) for(int col=n-1;col>=row;--col) { Cell& cell = board[row][col]; if(cell.searchId==searchCounter) { farmer.targetCell = &cell; routeFarmer(farmer); if(farmer.moved) return; } } if(DEBUG_FUNCTIONS) cerr<<"/MOVE_OUT "<<farmer.id<<endl; } void moveFarmerToGold(Unit& farmer) { if(DEBUG_FUNCTIONS) cerr<<"MOVE_FARMER_TO_GOLD "<<farmer.id<<endl; Cell* maxCell = &board[0][0]; bool currentContainsGold = farmer.cell->numberOfGold>0; int searchNumber = ++searchCounter; farmer.cell->searchId = searchNumber; vector<Cell*> v1 = {farmer.cell}, *current=&v1; vector<Cell*> v2, *future=&v2; int distance; for(distance=1;maxCell->numberOfGold==0 && !current->empty();swap(current, future),++distance) { future->clear(); for(Cell* cell : *current) for(pair<Cell*,bool>& neighbour : cell->neighbours) if(neighbour.first->canEnter() && neighbour.first->searchId!=searchNumber) { neighbour.first->searchId = searchNumber; future->push_back(neighbour.first); if(neighbour.first->numberOfGold>maxCell->numberOfGold) { if(!neighbour.second || neighbour.first->numberOfGold>farmer.cell->numberOfGold) maxCell = neighbour.first; } } } if(current->empty()) { if(DEBUG_MOVES) cerr << "Farmer " << farmer.id<<" did not find any gold in distance "<<distance<<endl; return; } if(DEBUG_MOVES) { cerr << "Farmer " << farmer.id<<" routes gold in "<<maxCell->row<<","<<maxCell->col<<endl; } farmer.targetCell = maxCell; (*maxCell) |= RESERVED_FLAG; ++reservedCells; routeFarmer(farmer); if(DEBUG_FUNCTIONS) cerr<<"/MOVE_FARMER_TO_GOLD "<<farmer.id<<endl; } void moveFarmerToBase(Unit& farmer) { if(DEBUG_FUNCTIONS) cerr<<"MOVE_FARMER_TO_BASE "<<farmer.id<<endl; farmer.targetCell = &board[0][0]; if(DEBUG_MOVES) { cerr << "Farmer " << farmer.id<<" goes to base"<<endl; } routeFarmer(farmer); if(DEBUG_FUNCTIONS) cerr<<"/MOVE_FARMER_TO_BASE "<<farmer.id<<endl; } void spawnTank() { goldInBase -= 100; board[0][0] |= TANK_FLAG; tanks.push_back(new Unit(&board[0][0])); if(PRINT_MOVES) cout << "R TANK\n"; } void spawnFarmer() { goldInBase -= 100; board[0][0] |= FARMER_FLAG; farmers.push_back(new Unit(&board[0][0])); if(PRINT_MOVES) cout << "R FARMER\n"; } void endTurn() { if(PRINT_MOVES) cout<<"=\n"; ++numberOfTurns; for(Unit* tank : tanks) { tank->moved=false; if(tank->cell->numberOfRocks>0) tank->cell->numberOfRocks = max(tank->cell->numberOfRocks-10, 0); } for(Unit* farmer : farmers) { farmer->moved=false; if(farmer->cell->numberOfGold>10) { farmer->gold += 10; farmer->cell->numberOfGold -= 10; } else if(farmer->cell->numberOfGold>0) { farmer->gold += farmer->cell->numberOfGold; farmer->cell->numberOfGold = 0; --cellsWithGold; } else if(farmer->cell == &board[0][0]) { goldInBase += farmer->gold; collectedGold += farmer->gold; farmer->gold = 0; } } if(DEBUG_MOVES) { for(Unit* farmer : farmers) { cerr << "Farmer "<<farmer->id<<" - " << farmer->cell->row<<","<<farmer->cell->col<<" -> " << farmer->gold<<endl; } REP(x,n) { REP(y,n) cerr<<board[x][y]<<" "; cerr<<endl; } } } void visit(Cell& cell) { if(cell.numberOfGold>=0 && !(cell & VISITED_FLAG)) { cell |= VISITED_FLAG; for(pair<Cell*,bool>& neighbour : cell.neighbours) visit(*neighbour.first); } } int getReachableGold() { visit(board[0][0]); int gold=0; cellsWithGold = 0; REP(row,n) REP(col,n) { Cell& cell = board[row][col]; if(cell.numberOfGold>0 && (cell & VISITED_FLAG)) { cell &= ~VISITED_FLAG; gold += cell.numberOfGold; ++cellsWithGold; } } return gold; } bool inline canEnterCell(int row, int col) { return board[row][col].canEnter(); } void solveOnlyFarmers(int goldToGet) { Cell* nearestCells[3] = {&board[0][0], &board[0][1], &board[1][0]}; bool b1, b2; while(goldToGet!=collectedGold) { while(goldInBase>=100 && farmers.size()<cellsWithGold && nearestCells[0]->canEnter() && ((b1=nearestCells[1]->canEnter())||(b2=nearestCells[2]->canEnter()))) { spawnFarmer(); if(b1 && b2) { moveFarmerToGold(*farmers.back()); } else if(b1) { moveFarmer(*farmers.back(), nearestCells[1]); } else /* if(b2) */ { moveFarmer(*farmers.back(), nearestCells[2]); } } for(Unit* farmer : farmers) { if(farmer->moved) continue; if(farmer->targetCell == farmer->cell) farmer->targetCell = nullptr; if(farmer->targetCell != nullptr) { routeFarmer(*farmer); } else if(farmer->gold>=100 && (farmers.size()<cellsWithGold)) moveFarmerToBase(*farmer); else if(reservedCells<cellsWithGold) moveFarmerToGold(*farmer); else if(farmer->gold>0 && farmer->cell->numberOfGold==0) moveFarmerToBase(*farmer); else if(farmer->gold==0) { moveOutFarmer(*farmer); farmer->targetCell = nullptr; } } endTurn(); } } void solve() { goldInBase = 200; collectedGold = 0; numberOfTurns = 0; reservedCells = 0; for(Unit* unit : farmers) delete unit; for(Unit* unit : tanks) delete unit; farmers.clear(); tanks.clear(); cin>>n; int sumOfGold = 0; REP(row,n) REP(col,n) { cin>>x; board[row][col].set(row, col, x, board); if(x>0) sumOfGold += x; } int goldToGet = getReachableGold(); if(DEBUG_FUNCTIONS) cerr << "to get: " << goldToGet<<" / "<<sumOfGold<<endl; if(goldToGet == sumOfGold) { solveOnlyFarmers(goldToGet); } if(PRINT_MOVES) cout<<"===\n"; if(COUNT_TURNS) cerr << "TURNS: " << numberOfTurns << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>T>>k; int sumTurns=0; REP(x,T) { solve(); sumTurns += numberOfTurns; } if(COUNT_TURNS) cerr << "AVERAGE TURNS: " << ceil(double(sumTurns)/T) << 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 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 | #include <iostream> #include <vector> #include <algorithm> #define REP(x,n) for(int x=0;x<(n);++x) using namespace std; int T,k,n,x; const int MAX_N = 20; int collectedGold; int goldInBase; int searchCounter=0; struct Cell; const int VISITED_FLAG = 0x010000; const int WORKER_FLAG = 0x020000; const int FARMER_FLAG = 0x040000 | WORKER_FLAG; const int TANK_FLAG = 0x080000 | WORKER_FLAG; const int RESERVED_FLAG =0x100000; const int ALL_FLAGS = 0x1F0000; const bool PRINT_MOVES=true; const bool DEBUG_MOVES=false; const bool DEBUG_FUNCTIONS=false; const bool COUNT_TURNS=true; int numberOfTurns; int cellsWithGold; int reservedCells; int unitCounter=0; struct Unit { Cell* cell; int gold; bool moved; Cell* targetCell; int id; Unit(Cell* cell): cell(cell), gold(0), moved(false), targetCell(nullptr), id(++unitCounter) { } }; struct Cell { int row; int col; int numberOfGold; int numberOfRocks; int flags; int searchId; int searchValue; int searchDistance; Cell* searchParent; vector<pair<Cell*,bool>> neighbours; Cell *leftNeighbour, *rightNeighbour, *topNeighbour, *bottomNeighbour; void set(int row, int col, int value, Cell board[MAX_N][MAX_N]) { leftNeighbour=rightNeighbour=topNeighbour=bottomNeighbour=nullptr; this->row = row; this->col = col; flags = 0; numberOfGold = max(0, value); numberOfRocks = max(0, -value); searchId = 0; neighbours.clear(); if(row>0) neighbours.push_back(make_pair(topNeighbour=&board[row-1][col], true)); if(col>0) neighbours.push_back(make_pair(leftNeighbour=&board[row][col-1], true)); if(row+1<n) neighbours.push_back(make_pair(bottomNeighbour=&board[row+1][col], false)); if(col+1<n) neighbours.push_back(make_pair(rightNeighbour=&board[row][col+1], false)); } int operator|(int value) const {return flags|value;} int operator&(int value) const {return flags&value;} int operator|=(int value) {return flags|=value;} int operator&=(int value) {return flags&=value;} bool isReserved() const {return flags&RESERVED_FLAG;} bool canEnter() const {return !(flags&WORKER_FLAG);}; }; ostream& operator<<(ostream& os, const Cell& cell) { int value = (cell.numberOfGold>0 ? cell.numberOfGold : -cell.numberOfRocks); string prefix = value>100 ? " " : value>10 ? " " : value>=0 ? " " : value>-10 ? " " : value>-100 ? " " : ""; os << prefix << value; if(cell.flags&TANK_FLAG) os << "T"; else if(cell.flags&FARMER_FLAG) os << "F"; else if(cell.flags&RESERVED_FLAG) os<<"R"; else os << " "; return os; } Cell board[MAX_N][MAX_N]; vector<Unit*> farmers, tanks; int inline sgn(int val) { return (val>0) - (val<0); } void moveTank(Unit& tank, Cell* target) { if(PRINT_MOVES) cout << "M "<<tank.cell->row<<" "<<tank.cell->col<<" "<<target->row<<" "<<target->col<<"\n"; (*tank.cell) &= ~TANK_FLAG; tank.cell=target; (*target) |= TANK_FLAG; tank.moved=true; } void moveFarmer(Unit& farmer, Cell* target) { if(DEBUG_FUNCTIONS) { cerr << "MOVE_FARMER " << farmer.id<<" -> "; if(target==nullptr) cerr << "[NULL]" << endl; else cerr << target->row<<","<<target->col<<endl; } if(PRINT_MOVES) cout << "M "<<farmer.cell->row<<" "<<farmer.cell->col<<" "<<target->row<<" "<<target->col<<"\n"; (*farmer.cell) &= ~FARMER_FLAG; farmer.cell=target; (*target) |= FARMER_FLAG; farmer.moved=true; if(DEBUG_FUNCTIONS) cerr << "/MOVE_FARMER" << endl; } void routeFarmer(Unit& farmer) { if(DEBUG_FUNCTIONS) cerr<<"ROUTE_FARMER "<<farmer.id<<" ["<<farmer.cell->row<<","<<farmer.cell->col<<"]"<<endl; if(farmer.moved) return; int searchNumber = ++searchCounter; farmer.cell->searchId = searchNumber; farmer.cell->searchValue = 0; farmer.cell->searchDistance = 0; vector<Cell*> v1 = {farmer.cell}, *current=&v1; vector<Cell*> v2, *future=&v2; int distance=0; for(bool stop=false;!stop && !current->empty();swap(current,future),++distance) { future->clear(); for(Cell* cell : *current) { for(pair<Cell*,bool>& neighbour : cell->neighbours) if(neighbour.first->canEnter()) { if(neighbour.first == farmer.targetCell) stop = true; if(neighbour.first->searchId!=searchNumber) { future->push_back(neighbour.first); neighbour.first->searchId = searchNumber; neighbour.first->searchDistance = cell->searchDistance+1; neighbour.first->searchValue = cell->searchValue + neighbour.first->numberOfGold; neighbour.first->searchParent = cell; } else if(neighbour.first->searchDistance>cell->searchDistance && neighbour.first->searchValue < cell->searchValue + neighbour.first->numberOfGold) { neighbour.first->searchValue = cell->searchValue + neighbour.first->numberOfGold; neighbour.first->searchParent = cell; } } } } Cell* nextCell = farmer.targetCell; if(current->empty() && distance==1) { if(DEBUG_MOVES) cerr << "path to "<<farmer.targetCell->row<<","<<farmer.targetCell->col<<" was not found"<<endl; return; } else if(current->empty()) { nextCell = future->back(); if(DEBUG_MOVES) cerr << "path to "<<farmer.targetCell->row<<","<<farmer.targetCell->col<<" was not found, going to closest reachable - "<<nextCell->row<<","<<nextCell->col<<endl; } for(;nextCell->searchParent != farmer.cell; nextCell = nextCell->searchParent); if(DEBUG_MOVES) cerr << "best path to "<<farmer.targetCell->row<<","<<farmer.targetCell->col<<" is via "<<nextCell->row<<","<<nextCell->col<<endl; moveFarmer(farmer, nextCell); if(farmer.cell == farmer.targetCell && (farmer.cell->flags & RESERVED_FLAG)) { farmer.cell->flags &= ~RESERVED_FLAG; --reservedCells; } if(DEBUG_FUNCTIONS) cerr<<"/ROUTE_FARMER "<<farmer.id<<endl; } void moveOutFarmer(Unit& farmer) { if(DEBUG_FUNCTIONS) cerr<<"===MOVE_OUT "<<farmer.id<<" ["<<farmer.cell->row<<","<<farmer.cell->col<<"]"<<endl; farmer.targetCell = &board[n-1][n-1]; routeFarmer(farmer); if(farmer.moved) return; for(int row=n-1;row>=0;--row) for(int col=n-1;col>=row;--col) { Cell& cell = board[row][col]; if(cell.searchId==searchCounter) { farmer.targetCell = &cell; routeFarmer(farmer); if(farmer.moved) return; } } if(DEBUG_FUNCTIONS) cerr<<"/MOVE_OUT "<<farmer.id<<endl; } void moveFarmerToGold(Unit& farmer) { if(DEBUG_FUNCTIONS) cerr<<"MOVE_FARMER_TO_GOLD "<<farmer.id<<endl; Cell* maxCell = &board[0][0]; bool currentContainsGold = farmer.cell->numberOfGold>0; int searchNumber = ++searchCounter; farmer.cell->searchId = searchNumber; vector<Cell*> v1 = {farmer.cell}, *current=&v1; vector<Cell*> v2, *future=&v2; int distance; for(distance=1;maxCell->numberOfGold==0 && !current->empty();swap(current, future),++distance) { future->clear(); for(Cell* cell : *current) for(pair<Cell*,bool>& neighbour : cell->neighbours) if(neighbour.first->canEnter() && neighbour.first->searchId!=searchNumber) { neighbour.first->searchId = searchNumber; future->push_back(neighbour.first); if(neighbour.first->numberOfGold>maxCell->numberOfGold) { if(!neighbour.second || neighbour.first->numberOfGold>farmer.cell->numberOfGold) maxCell = neighbour.first; } } } if(current->empty()) { if(DEBUG_MOVES) cerr << "Farmer " << farmer.id<<" did not find any gold in distance "<<distance<<endl; return; } if(DEBUG_MOVES) { cerr << "Farmer " << farmer.id<<" routes gold in "<<maxCell->row<<","<<maxCell->col<<endl; } farmer.targetCell = maxCell; (*maxCell) |= RESERVED_FLAG; ++reservedCells; routeFarmer(farmer); if(DEBUG_FUNCTIONS) cerr<<"/MOVE_FARMER_TO_GOLD "<<farmer.id<<endl; } void moveFarmerToBase(Unit& farmer) { if(DEBUG_FUNCTIONS) cerr<<"MOVE_FARMER_TO_BASE "<<farmer.id<<endl; farmer.targetCell = &board[0][0]; if(DEBUG_MOVES) { cerr << "Farmer " << farmer.id<<" goes to base"<<endl; } routeFarmer(farmer); if(DEBUG_FUNCTIONS) cerr<<"/MOVE_FARMER_TO_BASE "<<farmer.id<<endl; } void spawnTank() { goldInBase -= 100; board[0][0] |= TANK_FLAG; tanks.push_back(new Unit(&board[0][0])); if(PRINT_MOVES) cout << "R TANK\n"; } void spawnFarmer() { goldInBase -= 100; board[0][0] |= FARMER_FLAG; farmers.push_back(new Unit(&board[0][0])); if(PRINT_MOVES) cout << "R FARMER\n"; } void endTurn() { if(PRINT_MOVES) cout<<"=\n"; ++numberOfTurns; for(Unit* tank : tanks) { tank->moved=false; if(tank->cell->numberOfRocks>0) tank->cell->numberOfRocks = max(tank->cell->numberOfRocks-10, 0); } for(Unit* farmer : farmers) { farmer->moved=false; if(farmer->cell->numberOfGold>10) { farmer->gold += 10; farmer->cell->numberOfGold -= 10; } else if(farmer->cell->numberOfGold>0) { farmer->gold += farmer->cell->numberOfGold; farmer->cell->numberOfGold = 0; --cellsWithGold; } else if(farmer->cell == &board[0][0]) { goldInBase += farmer->gold; collectedGold += farmer->gold; farmer->gold = 0; } } if(DEBUG_MOVES) { for(Unit* farmer : farmers) { cerr << "Farmer "<<farmer->id<<" - " << farmer->cell->row<<","<<farmer->cell->col<<" -> " << farmer->gold<<endl; } REP(x,n) { REP(y,n) cerr<<board[x][y]<<" "; cerr<<endl; } } } void visit(Cell& cell) { if(cell.numberOfGold>=0 && !(cell & VISITED_FLAG)) { cell |= VISITED_FLAG; for(pair<Cell*,bool>& neighbour : cell.neighbours) visit(*neighbour.first); } } int getReachableGold() { visit(board[0][0]); int gold=0; cellsWithGold = 0; REP(row,n) REP(col,n) { Cell& cell = board[row][col]; if(cell.numberOfGold>0 && (cell & VISITED_FLAG)) { cell &= ~VISITED_FLAG; gold += cell.numberOfGold; ++cellsWithGold; } } return gold; } bool inline canEnterCell(int row, int col) { return board[row][col].canEnter(); } void solveOnlyFarmers(int goldToGet) { Cell* nearestCells[3] = {&board[0][0], &board[0][1], &board[1][0]}; bool b1, b2; while(goldToGet!=collectedGold) { while(goldInBase>=100 && farmers.size()<cellsWithGold && nearestCells[0]->canEnter() && ((b1=nearestCells[1]->canEnter())||(b2=nearestCells[2]->canEnter()))) { spawnFarmer(); if(b1 && b2) { moveFarmerToGold(*farmers.back()); } else if(b1) { moveFarmer(*farmers.back(), nearestCells[1]); } else /* if(b2) */ { moveFarmer(*farmers.back(), nearestCells[2]); } } for(Unit* farmer : farmers) { if(farmer->moved) continue; if(farmer->targetCell == farmer->cell) farmer->targetCell = nullptr; if(farmer->targetCell != nullptr) { routeFarmer(*farmer); } else if(farmer->gold>=100 && (farmers.size()<cellsWithGold)) moveFarmerToBase(*farmer); else if(reservedCells<cellsWithGold) moveFarmerToGold(*farmer); else if(farmer->gold>0 && farmer->cell->numberOfGold==0) moveFarmerToBase(*farmer); else if(farmer->gold==0) { moveOutFarmer(*farmer); farmer->targetCell = nullptr; } } endTurn(); } } void solve() { goldInBase = 200; collectedGold = 0; numberOfTurns = 0; reservedCells = 0; for(Unit* unit : farmers) delete unit; for(Unit* unit : tanks) delete unit; farmers.clear(); tanks.clear(); cin>>n; int sumOfGold = 0; REP(row,n) REP(col,n) { cin>>x; board[row][col].set(row, col, x, board); if(x>0) sumOfGold += x; } int goldToGet = getReachableGold(); if(DEBUG_FUNCTIONS) cerr << "to get: " << goldToGet<<" / "<<sumOfGold<<endl; if(goldToGet == sumOfGold) { solveOnlyFarmers(goldToGet); } if(PRINT_MOVES) cout<<"===\n"; if(COUNT_TURNS) cerr << "TURNS: " << numberOfTurns << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>T>>k; int sumTurns=0; REP(x,T) { solve(); sumTurns += numberOfTurns; } if(COUNT_TURNS) cerr << "AVERAGE TURNS: " << ceil(double(sumTurns)/T) << endl; return 0; } |