Grid Game
InterviewTime limit2sMemory limit512 MB
Each cell becomes alive if it or an edge neighbor was alive, so the live region grows by repeated dilation; count live cells after K steps.
- Level
Medium6 of 10
- Topics
- Simulation, BFS, Matrix, Implementation
- Solved
- No attempts yet
Problem
An infinite grid is divided into unit square cells. Every cell is either alive or dead.
Every second all cells change state at the same time, by this rule.
- Take the five cells made of itself and the four cells that share an edge with . If at least one of them is alive, then is alive one second later.
- Otherwise is dead one second later.
Given the starting state of the grid, write a program that counts how many cells are alive after seconds.
Input
The first line contains the number of rows and the number of columns of the rectangular area whose starting state is given. ()
Each of the next lines contains characters describing the starting state of that area. An alive cell is o and a dead cell is .. Every cell outside the area is dead at the start.
The last line contains . ()
Output
Print the number of alive cells after seconds.
Note
Suppose the starting state is the one below and .
oo
o.
After 3 seconds the grid looks like this.
...oo...
..oooo..
.oooooo.
oooooooo
ooooooo.
.ooooo..
..ooo...
...o....