# Game of Life

### Problem

According to the Wikipedia’s article: “The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.”

Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell interacts with its eight 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.

### Following 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?

### Thinking

这道题如果不要求（其实也只是建议）in-place的话就没有挑战性了，加入in-place的话，首先有两个想法，第一个是能不能找到一种顺序，使得后边的替换不会收到前边替换的结果的影响，这道题的话应该是不行的（emmmm…应该很多题都不行），然后另一个想法是看能不能找到一个滑动窗口或临时储值器之类的东西，保存当前运算完的结果，然后等到后边的用完前边的值之后再做更新，嗯，是可以的。然后就要考虑怎样使得这个储值器尽可能小，思考了一下，用一个一维数组就可以啦。

### Answer

1 | class Solution { |