#include #include #include #include using namespace std; struct animal; struct soil { int turns_since{ 0 }; bool has_grass{ false }; bool is_dead{ false }; void do_turn() { turns_since++; if (turns_since > 3 && !is_dead) { has_grass = true; } } void reset_grass() { has_grass = false; turns_since = 0; } char to_char() { if (has_grass) return '#'; if (is_dead) return '*'; return '.'; } }; int M; int N; int T; animal* animal_grid[20][20]{ {} }; struct animal { int row; int col; animal* other{ nullptr }; bool is_seep; int turns_since{ 1 }; animal(int pRow, int pCol, bool pIsSheep) : row{ pRow }, col{ pCol }, is_seep{pIsSheep} {}; virtual char to_char() = 0; virtual void move () = 0; }; struct sheep : public animal { sheep(int pRow, int pCol) : animal{ pRow, pCol, true } {}; virtual char to_char() { return 'S'; } virtual void move() { row++; turns_since++; if (row >= M) { row = 0; } } }; struct wolf : public animal { wolf(int pRow, int pCol) : animal{ pRow, pCol, false } {}; virtual char to_char() { return 'W'; } virtual void move() { ++col; if (col >= N) { col = 0; } turns_since++; } }; soil soil_grid[20][20]{}; void turn_soils() { for (int row = 0; row < M; row++) { for (int col = 0; col < N; col++) { soil_grid[row][col].do_turn(); } } } void move_animals() { vector animals; for (int row = 0; row < M; row++) { for (int col = 0; col < N; col++) { if (animal_grid[row][col]) { animals.push_back(animal_grid[row][col]); animal_grid[row][col]->move(); animal_grid[row][col] = nullptr; } } } for (animal* a : animals) { if (animal_grid[a->row][a->col]) { animal_grid[a->row][a->col]->other = a; } else { animal_grid[a->row][a->col] = a; } } } void eat_sheeps() { for (int row = 0; row < M; row++) { for (int col = 0; col < N; col++) { if (!animal_grid[row][col]) continue; if (!animal_grid[row][col]->other) continue; animal* a = animal_grid[row][col]; soil_grid[a->row][a->col].is_dead = true; soil_grid[a->row][a->col].has_grass = false; sheep* s; wolf* w; if (a->is_seep) { s = (sheep*)a; w = (wolf*)a->other; } else { s = (sheep*)a->other; w = (wolf*)a; } animal_grid[a->row][a->col] = w; w->other = nullptr; delete s; w->turns_since = 0; } } } void eat_grass() { for (int row = 0; row < M; row++) { for (int col = 0; col < N; col++) { if (animal_grid[row][col] && animal_grid[row][col]->is_seep && soil_grid[row][col].has_grass) { animal_grid[row][col]->turns_since = 1; soil_grid[row][col].has_grass = false; soil_grid[row][col].turns_since = 0; } } } } void print_result() { for (int row = 0; row < M; row++) { for (int col = 0; col < N; col++) { if (animal_grid[row][col]) { cout << animal_grid[row][col]->to_char(); } else { cout << soil_grid[row][col].to_char(); } } cout << '\n'; } } void starve_animals() { for (int row = 0; row < M; row++) { for (int col = 0; col < N; col++) { animal* a = animal_grid[row][col]; if (a) { if (a->is_seep) { if (a->turns_since >= 10) { delete a; animal_grid[row][col] = nullptr; soil_grid[row][col].has_grass = false; soil_grid[row][col].is_dead = true; } } else { if (a->turns_since >= 5) { delete a; animal_grid[row][col] = nullptr; soil_grid[row][col].has_grass = false; soil_grid[row][col].is_dead = true; } } } } cout << '\n'; } } int main() { string line; getline(cin, line); istringstream iline{ line }; iline >> T >> M >> N; for (int row = 0; row < M; row++) { getline(cin, line); int col{ 0 }; for (char c : line) { if ('.' == c) { ++col; continue; } if ('S' == c) animal_grid[row][col] = new sheep{ row, col }; if ('W' == c) animal_grid[row][col] = new wolf{ row, col }; ++col; } } for (int i = 0; i < T; i++) { turn_soils(); move_animals(); eat_sheeps(); eat_grass(); starve_animals(); cout << i << "turn:\n"; print_result(); cout << '\n'; } print_result(); return 0; }