题目:
根据 百度百科 , 生命游戏 ,简称为 生命 ,是英国数学家约翰·何顿·康威在 1970 年发明的细胞自动机。
给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞。每个细胞都具有一个初始状态: 1 即为 活细胞 (live),或 0 即为 死细胞 (dead)。每个细胞与其八个相邻位置(水平,垂直,对角线)的细胞都遵循以下四条生存定律:
如果活细胞周围八个位置的活细胞数少于两个,则该位置活细胞死亡;
如果活细胞周围八个位置有两个或三个活细胞,则该位置活细胞仍然存活;
如果活细胞周围八个位置有超过三个活细胞,则该位置活细胞死亡;
如果死细胞周围正好有三个活细胞,则该位置死细胞复活;
下一个状态是通过将上述规则同时应用于当前状态下的每个细胞所形成的,其中细胞的出生和死亡是同时发生的。给你 m x n 网格面板 board 的当前状态,返回下一个状态。
示例 1:
输入:board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
输出:[[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
示例 2:
输入:board = [[1,1],[1,0]]
输出:[[1,1],[1,1]]
提示:
m == board.length
n == board[i].length
1 <= m, n <= 25
board[i][j] 为 0 或 1
解题思路
1、单行或单列情况下只需要考虑细胞周围两个细胞状态
2、对细胞周围八个细胞存活计算
解题(只考虑执行速度未考虑内存消耗)
func gameOfLife(board [][]int) {
newboard := make([][]int, 0)
if len(board) == 1 { //行为1
newboard = append(newboard, make([]int, len(board[0])))
for k, v := range board[0] {
if k == 0 || k == len(board)-1 {
newboard[0][k] = 0
} else if k > 0 && k < len(board)-1 && board[0][k-1]+board[0][k+1] == 2 {
newboard[0][k] = v
}
}
} else {
for i := 0; i < len(board); i++ {
newboard = append(newboard, make([]int, len(board[i])))
if len(board[i]) ==1 { //列为1
newboard[i][0]=0
}else {
for j := 0; j < len(board[i]); j++ {
var livingCount int
//计算细胞周围八个细胞
if i != 0 && j != 0 && i != len(board)-1 && j != len(board[i])-1 {
livingCount = board[i-1][j-1] + board[i][j-1] + board[i+1][j-1] + board[i-1][j] + board[i+1][j] + board[i-1][j+1] + board[i][j+1] + board[i+1][j+1]
}
//判断细胞是否侵占了面板边界
if i == 0 {
if j == 0 {
livingCount = board[i+1][j] + board[i+1][j+1] + board[i][j+1]
} else if j == len(board[i])-1 {
livingCount = board[i][j-1] + board[i+1][j-1] + board[i+1][j]
} else {
livingCount = board[i][j-1] + board[i+1][j-1] + board[i+1][j] + board[i+1][j+1] + board[i][j+1]
}
}
if i == len(board)-1 {
if j == 0 {
livingCount = board[i-1][j] + board[i-1][j+1] + board[i][j+1]
} else if j == len(board[i])-1 {
livingCount = board[i-1][j-1] + board[i][j-1] + board[i-1][j]
} else {
livingCount = board[i-1][j-1] + board[i][j-1] + board[i-1][j] + board[i-1][j+1] + board[i][j+1]
}
}
if j == 0 {
if i == 0 {
livingCount = board[i+1][j] + board[i][j+1] + board[i+1][j+1]
} else if i == len(board)-1 {
livingCount = board[i-1][j] + board[i-1][j+1] + board[i][j+1]
} else {
livingCount = board[i-1][j] + board[i+1][j] + board[i-1][j+1] + board[i][j+1] + board[i+1][j+1]
}
}
if j == len(board[i])-1 {
if i == 0 {
livingCount = board[i][j-1] + board[i+1][j-1] + board[i+1][j]
} else if i == len(board)-1 {
livingCount = board[i-1][j-1] + board[i][j-1] + board[i-1][j]
} else {
livingCount = board[i-1][j-1] + board[i][j-1] + board[i+1][j-1] + board[i-1][j] + board[i+1][j]
}
}
//判断活细胞
if board[i][j] == 1 {
if livingCount < 2 { //如果活细胞周围八个位置的活细胞数少于两个,则该位置活细胞死亡;
newboard[i][j] = 0
}
if 2 == livingCount || livingCount == 3 { //如果活细胞周围八个位置有两个或三个活细胞,则该位置活细胞仍然存活;
newboard[i][j] = 1
}
if livingCount > 3 { //如果活细胞周围八个位置有超过三个活细胞,则该位置活细胞死亡;
newboard[i][j] = 0
}
} else if board[i][j] == 0 {
if livingCount == 3 { //如果死细胞周围正好有三个活细胞,则该位置死细胞复活;
newboard[i][j] = 1
}
}
}
}
}
}
copy(board, newboard)
fmt.Println(board)
}```