-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathleetcode289_game_of_life.cpp
96 lines (79 loc) · 2.47 KB
/
leetcode289_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
/**
* @file leetcode289_game_of_life.cpp
* @author wangguibao(https://github.com/wangguibao)
* @date 2023/07/01 17:45
* @brief https://leetcode.com/problems/game-of-life
*
* The inplace solution is from Stefan Pochmann (https://leetcode.com/StefanPochmann/)
* which can be found at
* https://leetcode.com/problems/game-of-life/solutions/73230/c-o-1-space-o-mn-time/
*/
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
void gameOfLife(vector<vector<int>>& board) {
int m = board.size();
int n = board[0].size();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
int neighborCnt = 0;
for (int k = std::max(i - 1, 0); k < std::min(i + 2, m); ++k) {
for (int l = std::max(j - 1, 0); l < std::min(j + 2, n); ++l) {
neighborCnt += board[k][l] & 1;
}
}
if (neighborCnt == 3 || neighborCnt - board[i][j] == 3) {
board[i][j] |= 2;
}
}
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
board[i][j] >>= 1;
}
}
}
};
void printMatrix(std::vector<std::vector<int>>& matrix) {
int row = matrix.size();
int col = matrix[0].size();
for (int i = 0; i < row; ++i) {
auto &r = matrix[i];
for (int j = 0; j < col; ++j) {
if (j > 0) {
std::cout << ' ';
}
std::cout << r[j];
}
std::cout << std::endl;
}
}
int main() {
while (1) {
int m = 0;
int n = 0;
std::cout << "Input m and n (Ctrl-C to exit): " << std::endl;
if (!(std::cin >> m >> n)) {
return 0;
}
std::vector<std::vector<int>> matrix;
for (int i = 0; i < m; ++i) {
std::cout << n << " integers for row " << i << std::endl;
std::vector<int> row;
int ele;
for (int i = 0; i < n; ++i) {
std::cin >> ele;
row.push_back(ele);
}
matrix.emplace_back(row);
}
printMatrix(matrix);
std::cout << "===============" << std::endl;
Solution solution;
solution.gameOfLife(matrix);
printMatrix(matrix);
}
return 0;
}