Problem 487
Game of Life
Medium · 20 pts · Matrix, Simulation
Problem
Conway's Game of Life is played on a grid of live (1) and dead (0) cells. Each cell has up to eight neighbours (horizontal, vertical and diagonal). All cells update at the same time using these rules. A live cell with fewer than two live neighbours dies. A live cell with two or three live neighbours stays alive. A live cell with more than three live neighbours dies. A dead cell with exactly three live neighbours becomes alive. Cells outside the grid count as dead. Print the grid after one step.
Input
First line: R and C. Next R lines: C values, each 0 or 1.
Output
Print the next state: R lines of C values.
Constraints
1 <= R, C <= 100
Example input
4 3 0 1 0 0 0 1 1 1 1 0 0 0
Example output
0 0 0 1 0 1 0 1 1 0 1 0
Explanation
In 0 1 0 / 0 0 1 / 1 1 1 / 0 0 0, the top live cell has only one live neighbour and dies, while the dead cell in the second row's first column has three live neighbours and becomes alive, among other changes.