# LeetCode 289 Game of Life（生命游戏）（Array）

NoMasp 分享于 2016-08-04

## 翻译

1，当前细胞为存活状态时，当周围低于2个（不包含2个）存活细胞时， 该细胞变成死亡状态。（模拟生命数量稀少）
2，当前细胞为存活状态时，当周围有2个或3个存活细胞时， 该细胞保持原样。
3，当前细胞为存活状态时，当周围有3个以上的存活细胞时，该细胞变成死亡状态。（模生命数量过多）
4，当前细胞为死亡状态时，当周围有3个存活细胞时，该细胞变成存活状态。 （模拟繁殖）

1，你可以就地解决吗？记住这空间需要同时更新：你不能只首先更新一部分，然后用这些新的状态去更新别的细胞。
2，在这个问题，我们使用了二维数组。原则上，这个空间是无限大的，当这些变化影响到边界时，你是如何解决的？

## 原文

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.

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?

## 分析

``if (count == 3 || (count == 2 && board[r][c] == 1)) board[r][c] += 10;``

## 代码

C Plus Plus

``````class Solution {
public:
void gameOfLife(vector<vector<int>>& board) {
for (int r = 0; r < board.size() ; r++) {
for (int c = 0; c < board[r].size(); c++) {
int count = countNeighbors(board, r, c);
if (count == 3 || (count == 2 && board[r][c] == 1)) board[r][c] += 10;
}
}
flashBoard(board);
}
int countNeighbors(const vector<vector<int>>& board, int r, int c) {
int count = 0;
for (int row  = max(r - 1, 0) ; row <= min(r + 1, (int) board.size() - 1); row ++) {
for (int col = max(c - 1, 0); col <= min(c + 1, (int) board[row].size() - 1); col ++) {
if (row != r || col != c) count += board[row][col] % 10;
}
}
return count;
}
void flashBoard(vector<vector<int>>& board) {
for (int i = 0; i < board.size(); i++) {
for (int j = 0; j < board[i].size(); j++) {
board[i][j] = board[i][j] / 10;
}
}
}
};``````

Java

``updated at 2016/09/04``
``````    public void gameOfLife(int[][] board) {
for (int r = 0; r < board.length; r++) {
for (int c = 0; c <board[r].length; c++) {
int count = countNeighbors(board, r, c);
if (count == 3 || (count == 2 && board[r][c] == 1))
board[r][c] += 10;
}
}
flashBoard(board);
}

int countNeighbors(int[][] board, int r, int c) {
int count = 0;
for (int row = Math.max(r - 1, 0); row <= Math.min(r + 1, (int)board.length - 1); row++) {
for (int col = Math.max(c - 1, 0); col <= Math.min(c + 1, (int)board[row].length - 1); col++) {
if (row != r || col != c)
count += board[row][col] % 10;
}
}
return count;
}

void flashBoard(int[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
board[i][j] = board[i][j] / 10;
System.out.println("b = " + board[i][j]);
}
}
}``````

×
• 登录
• 注册

×