-
Notifications
You must be signed in to change notification settings - Fork 12
/
solution.cpp
81 lines (76 loc) · 2.44 KB
/
solution.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
#include <vector>
#include <iostream>
using namespace std;
class Solution {
private:
int dx[8] = {-1, -1, -1, 0, 0, +1, +1, +1};
int dy[8] = {-1, 0, +1, -1, +1, -1, 0, +1};
bool isSafe(int x, int y, int N, int M) {
if (x < 0 || y < 0) {
return false;
}
if (x >= N || y >= M) {
return false;
}
return true;
}
public:
void gameOfLife(vector<vector<int>>& board) {
int N = board.size();
int M = board[0].size();
for (int row = 0; row < N; ++row) {
for (int col = 0; col < M; ++col) {
// count no. of live neighbors
int neighbors = 0;
for (int k = 0; k < 8; ++k) {
int x = row + dx[k];
int y = col + dy[k];
if (isSafe(x, y, N, M) && board[x][y] >= 1) {
++neighbors;
}
}
if (board[row][col] == 1) {
// update the cell with neighbors count
if (neighbors != 0) {
board[row][col] = neighbors;
}
} else {
// update the cell with -neighbors count
board[row][col] = -neighbors;
}
}
}
for (int row = 0; row < N; ++row) {
for (int col = 0; col < M; ++col) {
int neighbors = board[row][col];
if (neighbors > 0) {
if (neighbors < 2) { // live cell with < 2 live neighbors dies
board[row][col] = 0;
} else if (neighbors <= 3) { // with 2-3 lives
board[row][col] = 1;
} else { // dies due to over-population
board[row][col] = 0;
}
} else {
if (neighbors == -3) { // dead cell with 3 live neighbors
board[row][col] = 1;
} else {
board[row][col] = 0;
}
}
}
}
}
};
int main () {
vector<vector<int>> board = {{0, 1, 0}, {0, 0, 1}, {1, 1, 1}, {0, 0, 0}};
Solution solver;
solver.gameOfLife(board);
for (auto row: board) {
for (auto elem: row) {
cout << elem << " ";
}
cout << endl;
}
return 0;
}