Given aboardwithmbyncells, each cell has an initial statelive(1) ordead(0). Each cell interacts with itseight neighbors(horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):
Any live cell with fewer than two live neighbors dies, as if caused by under-population.
Any live cell with two or three live neighbors lives on to the next generation.
Any live cell with more than three live neighbors dies, as if by over-population..
Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
Write a function to compute the next state (after one update) of the board given its current state.
Follow up:
Could you solve it in-place? Remember that the board needs to be updated at the same time: You cannot update some cells first and then use their updated values to update other cells.
In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches the border of the array. How would you address these problems?
follow up 的问题关键在于怎么区别是原来就是活着或者死的, 还是被周围影响的变成活着或者死的。
解决的办法是借用两个值分别表示 0-->1 还是 1-->0 , 1-->1 没有任何变化,可以忽略这个条件。