#include #include using namespace std; struct Animal { int dirX, dirY; int x, y; int timeIndex = 0; bool dead = false; }; struct Tile { Animal * wolf = NULL; Animal * sheep = NULL; int timeIndex = 0; bool dead = false; bool grass = false; }; struct Board { Tile * tiles; int rows; int cols; Board(int rows, int cols) { this->rows = rows; this->cols = cols; tiles = new Tile[rows * cols]; for(int i = 0; i < rows * cols; ++i) { tiles[i] = { NULL, NULL, 0, false, false }; } } ~Board() { delete[] tiles; } Tile & getTile(int x, int y) { return tiles[y * cols + x]; } }; int main() { int numOfTurns, numOfRows, numOfColumns; cin >> numOfTurns >> numOfRows >> numOfColumns; Board board(numOfRows, numOfColumns); vector wolves; vector sheeps; char c; for(int y = 0; y < numOfRows; ++y) { for(int x = 0; x < numOfColumns; ++x) { cin >> c; if(c == 'S') { sheeps.push_back({0, 1, x, y, 0}); } else if (c == 'W') { wolves.push_back({1, 0, x, y, 0}); } } } for(int moveIndex = 0; moveIndex < numOfTurns; ++moveIndex) { for(int y = 0; y < numOfRows; ++y) { for(int x = 0; x < numOfColumns; ++x) { Tile & t = board.getTile(x, y); ++(t.timeIndex); if(t.dead == false && t.timeIndex >= 3) { t.grass = true; t.timeIndex = 0; } t.wolf = NULL; t.sheep = NULL; } } for(Animal & wolf : wolves) { if(wolf.dead) continue; ++wolf.timeIndex; if(wolf.timeIndex > 10) { wolf.dead = true; board.getTile(wolf.x, wolf.y).dead = true; } else { if(wolf.x + wolf.dirX >= numOfColumns) { wolf.dirX = -1; } else if(wolf.x + wolf.dirX < 0) { wolf.dirX = 1; } wolf.x += wolf.dirX; board.getTile(wolf.x, wolf.y).wolf = &wolf; } } for(Animal & sheep : sheeps) { if(sheep.dead) continue; ++sheep.timeIndex; if(sheep.timeIndex > 5) { sheep.dead = true; board.getTile(sheep.x, sheep.y).dead = true; } else { if(sheep.y + sheep.dirY >= numOfRows) { sheep.dirY = -1; } else if(sheep.y + sheep.dirY < 0) { sheep.dirY = 1; } sheep.y += sheep.dirY; Tile & t = board.getTile(sheep.x, sheep.y); if(t.wolf != NULL) { sheep.dead = true; t.wolf->timeIndex = 0; t.dead = true; } else if(t.grass == true && t.dead == false) { sheep.timeIndex = 0; t.sheep = &sheep; } else { t.sheep = &sheep; } } } } for(int y = 0; y < numOfRows; ++y) { for(int x = 0; x < numOfColumns; ++x) { Tile & t = board.getTile(x, y); if(t.wolf != NULL) { cout << "W"; } else if(t.sheep != NULL) { cout << "S"; } else if(t.dead) { cout << "*"; } else if(t.grass) { cout << "#"; } else { cout << "."; } } cout << endl; } return 0; }