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. The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously.
Example:
Input:
[
[0,1,0],
[0,0,1],
[1,1,1],
[0,0,0]
]
Output:
[
[0,0,0],
[1,0,1],
[0,1,1],
[0,1,0]
]
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?
这道题目最简单的解法就是拷贝一份棋盘,然后修改数据,如果不拷贝棋盘,那么需要多遍历一遍,把2作为0变为1后复活的值,把-1作为1变为0后死亡的值,这样就可以标记所有数据了。
Java解法如下:
class Solution {
public void gameOfLife(int[][] board) {
// Neighbors array to find 8 neighboring cells for a given cell
int[] neighbors = {0, 1, -1};
int rows = board.length;
int cols = board[0].length;
// Iterate through board cell by cell.
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
// For each cell count the number of live neighbors.
int liveNeighbors = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (!(neighbors[i] == 0 && neighbors[j] == 0)) {
int r = (row + neighbors[i]);
int c = (col + neighbors[j]);
// Check the validity of the neighboring cell.
// and whether it was originally a live cell.
if ((r < rows && r >= 0) && (c < cols && c >= 0) && (Math.abs(board[r][c]) == 1)) {
liveNeighbors += 1;
}
}
}
}
// Rule 1 or Rule 3
if ((board[row][col] == 1) && (liveNeighbors < 2 || liveNeighbors > 3)) {
// -1 signifies the cell is now dead but originally was live.
board[row][col] = -1;
}
// Rule 4
if (board[row][col] == 0 && liveNeighbors == 3) {
// 2 signifies the cell is now live but was originally dead.
board[row][col] = 2;
}
}
}
// Get the final representation for the newly updated board.
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
if (board[row][col] > 0) {
board[row][col] = 1;
} else {
board[row][col] = 0;
}
}
}
}
}
Python解法如下:
class Solution:
def gameOfLife(self, board: List[List[int]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
# Neighbors array to find 8 neighboring cells for a given cell
neighbors = [(1,0), (1,-1), (0,-1), (-1,-1), (-1,0), (-1,1), (0,1), (1,1)]
rows = len(board)
cols = len(board[0])
# Iterate through board cell by cell.
for row in range(rows):
for col in range(cols):
# For each cell count the number of live neighbors.
live_neighbors = 0
for neighbor in neighbors:
# row and column of the neighboring cell
r = (row + neighbor[0])
c = (col + neighbor[1])
# Check the validity of the neighboring cell and if it was originally a live cell.
if (r < rows and r >= 0) and (c < cols and c >= 0) and abs(board[r][c]) == 1:
live_neighbors += 1
# Rule 1 or Rule 3
if board[row][col] == 1 and (live_neighbors < 2 or live_neighbors > 3):
# -1 signifies the cell is now dead but originally was live.
board[row][col] = -1
# Rule 4
if board[row][col] == 0 and live_neighbors == 3:
# 2 signifies the cell is now live but was originally dead.
board[row][col] = 2
# Get the final representation for the newly updated board.
for row in range(rows):
for col in range(cols):
if board[row][col] > 0:
board[row][col] = 1
else:
board[row][col] = 0

这篇博客介绍了LeetCode上的289题——生命游戏,这是一个由John Horton Conway在1970年提出的细胞自动机问题。文章详细解释了游戏的四个生存规则,并提供了使用Java和Python解决此问题的代码示例,探讨了如何在不复制棋盘的情况下同步更新所有单元格状态的挑战。

被折叠的 条评论
为什么被折叠?



