-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy path289. Game of Life.cpp
100 lines (88 loc) · 2.98 KB
/
289. Game of Life.cpp
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
/*
Transition: Marks
0 -> 0 : 0
1 -> 1 : 1
0 -> 1 : -1
1 -> 0 : 2
Then update the board according to their marks.
*/
class Solution {
public:
void gameOfLife(vector<vector<int>>& board) {
int m = board.size() - 1, n = board[0].size() - 1;
for(int i = 0; i <= m; i++)
for(int j = 0; j <= n; j++){
int neigh = cntNeighbor(board, m, n, i, j), cur = board[i][j];
board[i][j] = (neigh > 3 || neigh < 2) && cur ? 2 : (neigh == 3 && !cur) ? -1 : cur;
}
for(auto& x: board)
for(auto& y: x)
y = (y == 2) ? 0 : (y == -1) ? 1 : y;
}
int cntNeighbor(vector<vector<int>>& board, int m, int n, int r, int c){
int cnt = 0;
for(int i = max(0, r - 1); i <= min(m, r + 1); i++)
for(int j = max(0, c - 1); j <= min(n, c + 1); j++)
if((i != r || j != c) && board[i][j] > 0) cnt++;
return cnt;
}
};
// Or use the 2-bit to store the new state, idea from Stefan's post:(https://discuss.leetcode.com/topic/26112/c-o-1-space-o-mn-time).
class Solution {
public:
void gameOfLife(vector<vector<int>>& board) {
int m = board.size() - 1, n = board[0].size() - 1;
for(int i = 0; i <= m; i++)
for(int j = 0; j <= n; j++){
int neigh = cntNeighbor(board, m, n, i, j);
if((neigh == 2 && board[i][j]) || neigh == 3) board[i][j] |= 2;
}
for(auto& x: board)
for(auto& y: x)
y >>= 1;
}
int cntNeighbor(vector<vector<int>>& board, int m, int n, int r, int c){
int cnt = 0;
for(int i = max(0, r - 1); i <= min(m, r + 1); i++)
for(int j = max(0, c - 1); j <= min(n, c + 1); j++)
if((i != r || j != c) && board[i][j] & 1) cnt++;
return cnt;
}
};
class Solution {
public:
void gameOfLife(vector<vector<int>>& board) {
int m = board.size(), n = board[0].size();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
int count = getNeighbor(board, i, j);
if (count == 3){
board[i][j] |= 2;
} else if (count == 2){
int tmp = board[i][j];
board[i][j] <<= 1;
board[i][j] |= tmp;
}
}
}
for (auto& v: board) {
for (int& x: v) {
x >>= 1;
}
}
}
int getNeighbor(vector<vector<int>>& board, int r, int c) {
int count = 0, m = board.size(), n = board[0].size();
for (int i = max(0, r - 1); i <= min(r + 1, m - 1); ++i) {
for (int j = max(0, c - 1); j <= min(c + 1, n - 1); ++j) {
if (i == r && j == c) {
continue;
}
if (board[i][j] & 1 == 1) {
++count;
}
}
}
return count;
}
};