289. 生命游戏
为保证权益,题目请参考 289. 生命游戏(From LeetCode).
解决方案1
CPP
C++
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
// leetcode 289
class Solution {
public:
void gameOfLife(vector<vector<int>> &board) {
vector<vector<int>> board_copy(board);
for (int i = 0; i < board.size(); ++i) {
for (int j = 0; j < board[0].size(); ++j) {
int num = 0;
if (i - 1 >= 0) {
num += board[i - 1][j];
if (j - 1 >= 0) {
num += board[i - 1][j - 1];
}
}
if (j - 1 >= 0) {
num += board[i][j - 1];
if (i + 1 < board.size()) {
num += board[i + 1][j - 1];
}
}
if (i + 1 < board.size()) {
num += board[i + 1][j];
if (j + 1 < board[0].size()) {
num += board[i + 1][j + 1];
}
}
if (j + 1 < board[0].size()) {
num += board[i][j + 1];
if (i - 1 >= 0) {
num += board[i - 1][j + 1];
}
}
if (board[i][j] == 1) {
if (num < 2 || num > 3) {
board_copy[i][j] = 0;
}
} else {
if (num == 3) {
board_copy[i][j] = 1;
}
}
}
}
for (int i = 0; i < board.size(); ++i) {
for (int j = 0; j < board[0].size(); ++j) {
board[i][j] = board_copy[i][j];
}
}
}
};
int main() {
return 0;
}
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
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
Python
python
from typing import List
class Solution:
def isAlive(self, cell):
return cell & 1
def nextAlive(self, cell):
return cell | 2
def nextDied(self, cell):
return cell | 1
def isNextAlive(self, cell):
return cell & 2
def getLiveNum(self, board: List[List[int]], i, j):
"""
获取细胞周围的活细胞
"""
count = 0
top = True if i - 1 >= 0 else False
bottom = True if i + 1 < len(board) else False
left = True if j - 1 >= 0 else False
right = True if j + 1 < len(board[0]) else False
if top:
count += self.isAlive(board[i-1][j])
if left:
count += self.isAlive(board[i-1][j-1])
if right:
count += self.isAlive(board[i-1][j+1])
if bottom:
count += self.isAlive(board[i+1][j])
if left:
count += self.isAlive(board[i+1][j-1])
if right:
count += self.isAlive(board[i+1][j+1])
if left:
count += self.isAlive(board[i][j-1])
if right:
count += self.isAlive(board[i][j+1])
return count
def gameOfLife(self, board: List[List[int]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
for i in range(len(board)):
for j in range(len(board[0])):
n = self.getLiveNum(board, i, j)
if self.isAlive(board[i][j]):
if n < 2 or n > 3:
board[i][j] = self.nextDied(board[i][j])
else:
board[i][j] = self.nextAlive(board[i][j])
else:
if n == 3:
board[i][j] = self.nextAlive(board[i][j])
for i in range(len(board)):
for j in range(len(board[0])):
if self.isNextAlive(board[i][j]):
board[i][j] = 1
else:
board[i][j] = 0
def runTest():
so = Solution()
a = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
so.gameOfLife(a)
print(a)
runTest()
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
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