fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. enum class Cell
  5. {
  6. RED, BLUE, NONE
  7. };
  8.  
  9. class GameOfLife
  10. {
  11. private:
  12. int width;
  13. int height;
  14. std::vector<std::vector<Cell>> grid;
  15.  
  16. void setCell(int x, int y, Cell value)
  17. {
  18. grid[y][x] = value;
  19. }
  20.  
  21. Cell cellAt(int x, int y) const
  22. {
  23. if(x >= width)
  24. x %= width;
  25. if(y >= height)
  26. y %= height;
  27. if(x < 0)
  28. x += width;
  29. if(y < 0)
  30. y += height;
  31. return grid[y][x];
  32. }
  33.  
  34. int countAlive(int x, int y, int& redAlive, int& blueAlive) const
  35. {
  36. blueAlive = 0;
  37. redAlive = 0;
  38. for(int j = y - 1; j <= y + 1; j++)
  39. {
  40. for(int i = x - 1; i <= x + 1; i++)
  41. {
  42. if(i == x && y == j)
  43. continue;
  44. if(cellAt(i, j) == Cell::RED)
  45. redAlive++;
  46. else if(cellAt(i, j) == Cell::BLUE)
  47. blueAlive++;
  48. }
  49. }
  50. return blueAlive + redAlive;
  51. }
  52.  
  53. Cell findColor(Cell color, int redAlive, int blueAlive) const
  54. {
  55. if(color == Cell::RED)
  56. return redAlive + 1 > blueAlive ? Cell::RED : Cell::BLUE;
  57. if(color == Cell::BLUE)
  58. return blueAlive + 1 > redAlive ? Cell::BLUE : Cell::RED;
  59. throw "This cell should not be dead";
  60. }
  61.  
  62. public:
  63. GameOfLife(std::vector<std::vector<Cell>> grid)
  64. {
  65. this->grid = grid;
  66. width = grid[0].size();
  67. height = grid.size();
  68. }
  69.  
  70. void advanceSimulation()
  71. {
  72. for(int j = 0; j < height; j++)
  73. {
  74. for(int i = 0; i < width; i++)
  75. {
  76. int redAlive, blueAlive;
  77. int alive = countAlive(i, j, redAlive, blueAlive);
  78. Cell cell = cellAt(i, j);
  79.  
  80. if(cell == Cell::NONE)
  81. {
  82. if(alive == 3)
  83. setCell(i, j, redAlive > blueAlive ? Cell::RED : Cell::BLUE);
  84. }
  85. else
  86. {
  87.  
  88. if(alive < 2 || alive > 3)
  89. setCell(i, j, Cell::NONE);
  90. else
  91. setCell(i, j, findColor(cell, redAlive, blueAlive));
  92. }
  93. }
  94. }
  95. }
  96.  
  97. void print() const
  98. {
  99. for(const auto& row: grid)
  100. {
  101. for(const auto& cell: row)
  102. {
  103. switch(cell)
  104. {
  105. case Cell::RED: std::cout << '#'; break;
  106. case Cell::BLUE: std::cout << '*'; break;
  107. case Cell::NONE: std::cout << '.'; break;
  108. }
  109. }
  110. std::cout << std::endl;
  111. }
  112. std::cout << std::endl;
  113. }
  114. };
  115.  
  116. std::ostream& operator<<(std::ostream& out, const GameOfLife& game)
  117. {
  118. return out;
  119. }
  120.  
  121. int main()
  122. {
  123. int width, height, N;
  124. std::cin >> width >> height >> N;
  125. std::vector<std::vector<Cell>> grid;
  126. grid.reserve(height);
  127. std::string line;
  128. getline(std::cin, line);
  129. for(int h = 0; h < height; h++)
  130. {
  131. std::vector<Cell> row;
  132. row.reserve(width);
  133. getline(std::cin, line);
  134. for(int w = 0; w < width; w++)
  135. {
  136. switch(line[w])
  137. {
  138. case '#': row.push_back(Cell::RED); break;
  139. case '*': row.push_back(Cell::BLUE); break;
  140. case '.': row.push_back(Cell::NONE); break;
  141. }
  142. }
  143. grid.push_back(row);
  144. }
  145. GameOfLife game(grid);
  146. game.print();
  147. while(N--)
  148. {
  149. game.advanceSimulation();
  150. game.print();
  151. }
  152.  
  153. return 0;
  154. }
Success #stdin #stdout 0s 16056KB
stdin
5 5 5
.*.**
.#...
##..#
...##
.*...
stdout
.*.**
.#...
##..#
...##
.*...

.*...
.##..
.#..#
..###
**.#.

.....
###..
....#
.*.#.
**...

..#..
#####
.....
***..
*.***

.....
#####
.....
*.*..
*.***

.....
#####
.....
*.*..
*.***