-
Notifications
You must be signed in to change notification settings - Fork 0
/
200711-1.cpp
61 lines (58 loc) · 1.35 KB
/
200711-1.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
// https://leetcode-cn.com/problems/game-of-life/
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
void gameOfLife(vector<vector<int>>& board) {
int n = board.size();
int m = board[0].size();
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
int x = 0;
if (i > 0 && j > 0) x += (board[i - 1][j - 1] % 2);
if (i > 0 ) x += (board[i - 1][j ] % 2);
if (i > 0 && j + 1 < m) x += (board[i - 1][j + 1] % 2);
if ( j > 0) x += (board[i ][j - 1] % 2);
if ( j + 1 < m) x += (board[i ][j + 1] % 2);
if (i + 1 < n && j > 0) x += (board[i + 1][j - 1] % 2);
if (i + 1 < n ) x += (board[i + 1][j ] % 2);
if (i + 1 < n && j + 1 < m) x += (board[i + 1][j + 1] % 2);
if ((x == 2 && (board[i][j] % 2)) || x == 3) {
board[i][j] |= 2;
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
board[i][j] /= 2;
}
}
}
};
void print(const vector<vector<int>>& a) {
cout << "[" << endl;
for (const auto& r : a) {
cout << " [";
for (auto e : r) {
cout << " " << e;
}
cout << " ]" << endl;
}
cout << "]" << endl;
}
int main()
{
Solution s;
{
vector<vector<int>> a = {
{0,1,0},
{0,0,1},
{1,1,1},
{0,0,0}
};
s.gameOfLife(a);
print(a);
}
return 0;
}