★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10241266.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."

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 }

转载于:https://www.cnblogs.com/strengthen/p/10241266.html

[Swift]LeetCode289. 生命游戏 | Game of Life相关推荐

  1. Leetcode--289. 生命游戏

    根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有一个初始状态 liv ...

  2. 【网格问题】leetcode289.生命游戏

    题目: 根据 百度百科 , 生命游戏 ,简称为 生命 ,是英国数学家约翰·何顿·康威在 1970 年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞 ...

  3. leetCode-289. 生命游戏

    题目: 根据 百度百科 ,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在 1970 年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞都具有一 ...

  4. 伍六七带你学算法 进阶篇-生命游戏

    有趣的算法题–生命游戏 难度-中等 根据 百度百科 ,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在 1970 年发明的细胞自动机. 想要体验生命游戏的小伙伴可以到这里-->生命游戏 进入 ...

  5. html5 生存游戏,html5版生命游戏

    html5版生命游戏 只有一个.html文件,无任何依赖. 使用canvas 1.[文件] index.html ~ 4KB     下载(206) canvas { background: #eee ...

  6. Swift版iOS游戏框架Sprite Kit基础教程下册

    Swift版iOS游戏框架Sprite Kit基础教程下册 试读下载地址:http://pan.baidu.com/s/1qWBdV0C  介绍:本教程是国内唯一的Swift版的Spritekit教程 ...

  7. 康威生命游戏是如何搭建计算机的?

    2020年4月,数学家约翰·康威(John H. Conway)因新冠肺炎去世.大家回顾康威教授平生贡献时,不可避免要提到伟大.深刻的"康威生命游戏"(Conway's Game ...

  8. 生命游戏(Game of Life)描述

    一.生命游戏(Game of Life)描述 生命游戏是英国数学家约翰·何顿·康威在1970年发明的细胞自动机,它包括一个二维矩形世界,这个世界中的每个方格居住着一个活着的或死亡的细胞.一个细胞在下一 ...

  9. 细胞计算机生命游戏,【图片】【20170108 其它內容】【转】生命游戏【三体吧】_百度贴吧...

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 有些初始图案最终会得到稳定不变的图形,康威把这些稳定图形叫做"静物"(still life).常见的静物有:方块.小船.面包.蜂巢等( ...

最新文章

  1. 2022-2028年中国体育用品行业投资分析及前景预测报告(全卷)
  2. Task04:青少年软件编程(Scratch)等级考试模拟卷(一级)
  3. 【C#串口编程计划】如何避免关闭串口时软件死锁
  4. 美国新规:自动驾驶车,从此不需要驾驶位了
  5. 不想学python-没想到,学会Python即使不做程序员都能月入过万!
  6. python︱HTML网页解析BeautifulSoup学习笔记
  7. “三峡水怪”的真面目竟是这个!水怪:我不要面子的吗?
  8. mysql如何查询不等于_mysql查询不等于
  9. Android开发实践:线程与异步任务
  10. revit 转换ifc_导出 IFC 文件以使用 BIM 软件进行编辑
  11. Web前端-HTML基础
  12. Sqlite3相关函数返回值及其含义
  13. Linux文件压缩命令
  14. PCI 总线及地址空间
  15. vue中使用loadsh实现防抖功能及处理各种数据
  16. Qt编写自定义控件54-时钟仪表盘
  17. 基于Opencv的图像处理-高光调整算法
  18. MySQL安装版本Navicat连接报错2509解决方案
  19. 匈牙利命名法(Hungarian Notation)
  20. C语言基础代码合集 | 十进制转化为二进制

热门文章

  1. 在CentOS下搭建自己的Git服务器
  2. Visual Studio 2015编译wxWidgets
  3. SDNLAB技术分享(二):从Toaster示例初探ODL MD-SAL架构
  4. CentOS+nginx+uwsgi+Python 多站点环境搭建
  5. 程序员的量化交易之路(17)--Cointrader之Temporal实体(5)
  6. Oracle RMAN Catalog 和 Nocatalog 的区别
  7. [详细功能介绍]Stimulsoft报表全线更新至2012.3
  8. freeswitch 查看当前注册用户命令
  9. 某化工学院安装锐捷elog
  10. python学习笔记——多线程编程