在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ 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):
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:
根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机。 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞。每个细胞具有一个初始状态 live(1)即为活细胞, 或 dead(0)即为死细胞。每个细胞与其八个相邻位置(水平,垂直,对角线)的细胞都遵循以下四条生存定律:
根据当前状态,写一个函数来计算面板上细胞的下一个(一次更新后的)状态。下一个状态是通过将上述规则同时应用于当前状态下的每个细胞所形成的,其中细胞的出生和死亡是同时发生的。 示例: 输入: [ [0,1,0], [0,0,1], [1,1,1], [0,0,0] ] 输出: [ [0,0,0], [1,0,1], [0,1,1], [0,1,0] ] 进阶:
12ms 1 class Solution { 2 func gameOfLife(_ board: inout [[Int]]) { 3 4 let m = board.count 5 let n = board[0].count 6 7 for i in 0..<m { 8 for j in 0..<n { 9 var count = 0 10 if i != 0 { 11 if j != 0 { 12 count += board[i-1][j-1] > 0 ? 1 : 0 13 } 14 15 if j != n-1 { 16 count += board[i-1][j+1] > 0 ? 1 : 0 17 } 18 19 count += board[i-1][j] > 0 ? 1 : 0 20 } 21 22 if i != m-1 { 23 if j != 0 { 24 count += board[i+1][j-1] > 0 ? 1 : 0 25 } 26 27 if j != n-1 { 28 count += board[i+1][j+1] > 0 ? 1 : 0 29 } 30 31 count += board[i+1][j] > 0 ? 1 : 0 32 } 33 34 if j != 0 { 35 count += board[i][j-1] > 0 ? 1 : 0 36 } 37 38 if j != n-1 { 39 count += board[i][j+1] > 0 ? 1 : 0 40 } 41 42 if board[i][j] == 0 { 43 if count == 3 { 44 board[i][j] = -1 45 } 46 }else { 47 if count != 2 && count != 3 { 48 board[i][j] = 2 49 } 50 } 51 } 52 } 53 54 for i in 0..<m { 55 for j in 0..<n { 56 if board[i][j] == 2 { 57 board[i][j] = 0 58 } 59 if board[i][j] == -1 { 60 board[i][j] = 1 61 } 62 } 63 } 64 } 65 } 20ms 1 class Solution { 2 func gameOfLife(_ board: inout [[Int]]) { 3 var m = board.count 4 var n = board[0].count 5 var matrix = board 6 7 func helper(_ i: Int, _ j: Int) -> Int { 8 var count = 0 9 if i > 0 { 10 count += board[i - 1][j] 11 if j > 0 { 12 count += board[i - 1][j - 1] 13 } 14 if j < n - 1 { 15 count += board[i - 1][j + 1] 16 } 17 } 18 if i < m - 1 { 19 count += board[i + 1][j] 20 if j > 0 { 21 count += board[i + 1][j - 1] 22 } 23 if j < n - 1 { 24 count += board[i + 1][j + 1] 25 } 26 } 27 if j > 0 { 28 count += board[i][j - 1] 29 } 30 if j < n - 1 { 31 count += board[i][j + 1] 32 } 33 34 if board[i][j] == 1 { 35 if count == 2 || count == 3 { 36 return 1 37 } else { 38 return 0 39 } 40 } else { 41 if count == 3 { 42 return 1 43 } else { 44 return 0 45 } 46 } 47 } 48 49 for i in 0 ..< m { 50 for j in 0 ..< n { 51 matrix[i][j] = helper(i, j) 52 } 53 } 54 board = matrix 55 } 56 } 32ms 1 class Solution { 2 func gameOfLife(_ board: inout [[Int]]) { 3 guard board.count > 0 else { 4 return 5 } 6 7 let m = board.count, n = board[0].count 8 for i in 0..<m { 9 for j in 0..<n { 10 changeStatus(&board, i, j, m, n) 11 } 12 } 13 board = board.map { $0.map{ $0 % 2 } } 14 print(board) 15 } 16 17 func changeStatus(_ board: inout [[Int]], _ i: Int, _ j: Int, _ m: Int, _ n: Int) { 18 var liveNum = 0 19 20 for x in (i - 1)...(i + 1) { 21 for y in (j - 1)...(j + 1) { 22 if x < 0 || x >= m || y < 0 || y >= n { 23 continue 24 } 25 26 if x == i && y == j { 27 continue 28 } 29 30 liveNum = board[x][y] == 1 || board[x][y] == 2 ? liveNum + 1 : liveNum 31 } 32 } 33 34 if board[i][j] == 1 { 35 board[i][j] = (liveNum < 2 || liveNum > 3) ? 2 : 1 36 } else { 37 board[i][j] = liveNum == 3 ? 3 : 0 38 } 39 } 40 }
|
请发表评论