[Swift]LeetCode289. 生命游戏 | Game of Life

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-qvrgpcjv-kw.html 
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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."git

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):github

  1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.
  2. Any live cell with two or three live neighbors lives on to the next generation.
  3. Any live cell with more than three live neighbors dies, as if by over-population..
  4. 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:微信

  1. 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.
  2. 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?

根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机。app

给定一个包含 m × n 个格子的面板,每个格子均可以当作是一个细胞。每一个细胞具备一个初始状态 live(1)即为活细胞, 或 dead(0)即为死细胞。每一个细胞与其八个相邻位置(水平,垂直,对角线)的细胞都遵循如下四条生存定律:函数

  1. 若是活细胞周围八个位置的活细胞数少于两个,则该位置活细胞死亡;
  2. 若是活细胞周围八个位置有两个或三个活细胞,则该位置活细胞仍然存活;
  3. 若是活细胞周围八个位置有超过三个活细胞,则该位置活细胞死亡;
  4. 若是死细胞周围正好有三个活细胞,则该位置死细胞复活;

根据当前状态,写一个函数来计算面板上细胞的下一个(一次更新后的)状态。下一个状态是经过将上述规则同时应用于当前状态下的每一个细胞所造成的,其中细胞的出生和死亡是同时发生的。this

示例:spa

输入: 
[
  [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 }
相关文章
相关标签/搜索