• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode289.生命游戏|GameofLife

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10241266.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新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):

  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年发明的细胞自动机。

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

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

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

示例:

输入: 
[
  [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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
从Java到Swift发布时间:2022-07-13
下一篇:
Swift游戏实战-跑酷熊猫03熊猫跑动动画发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap