-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
116 lines (85 loc) · 3.25 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//: [上一道题](@previous)
/*:
# 生命游戏
- 题号:[289](https://leetcode-cn.com/problems/game-of-life/)
- 难度:中等
- 描述:
给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞。
每个细胞都具有一个初始状态:1 即为活细胞(live),或 0 即为死细胞(dead)。
每个细胞与其八个相邻位置(水平,垂直,对角线)的细胞都遵循以下四条生存定律:
1. 如果活细胞周围八个位置的活细胞数少于两个,则该位置活细胞死亡;
2. 如果活细胞周围八个位置有两个或三个活细胞,则该位置活细胞仍然存活;
3. 如果活细胞周围八个位置有超过三个活细胞,则该位置活细胞死亡;
4. 如果死细胞周围正好有三个活细胞,则该位置死细胞复活;
根据当前状态,写一个函数来计算面板上所有细胞的下一个(一次更新后的)状态。
下一个状态是通过将上述规则同时应用于当前状态下的每个细胞所形成的,其中细胞的出生和死亡是同时发生的。
*/
//: ## Code
import Foundation
func gameOfLife(_ board: inout [[Int]]) {
var result = board
for i in 0 ..< board.count {
for j in 0 ..< board[i].count {
// 活细胞数量
let count = getLiveCellCount(from: board, i: i, j: j)
if board[i][j] == 0 { // 死细胞
// 周围正好有三个活细胞,则该位置死细胞复活
if count == 3 {
result[i][j] = 1
}
} else { // 活细胞
// 1. 周围的活细胞数少于两个,则该活细胞死亡
// 2. 周围有两个或三个活细胞,则该活细胞仍然存活
// 3. 周围有超过三个活细胞,则该活细胞死亡
if count < 2 || count > 3 {
result[i][j] = 0
}
}
}
}
board = result
}
private func getLiveCellCount(from board: [[Int]], i: Int, j: Int) -> Int {
// 记录活细胞数量
var count = 0
if i > 0 {
// 左
if board[i - 1][j] == 1 { count += 1 }
// 左上
if j > 0 && board[i - 1][j - 1] == 1 {
count += 1
}
// 左下
if j + 1 < board[i - 1].count && board[i - 1][j + 1] == 1 {
count += 1
}
}
if i + 1 < board.count {
// 右
if board[i + 1][j] == 1 {
count += 1
}
// 右上
if j > 0 && board[i + 1][j - 1] == 1 {
count += 1
}
// 右下
if j + 1 < board[i].count && board[i + 1][j + 1] == 1 {
count += 1
}
}
// 上
if j > 0 && board[i][j - 1] == 1 {
count += 1
}
// 下
if j + 1 < board[i].count && board[i][j + 1] == 1 {
count += 1
}
return count
}
//: ## Test
var test1 = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
gameOfLife(&test1)
print(test1)
//: [下一道题](@next)