forked from iamAnki/CPP-Programs-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame Of Life.cpp
36 lines (28 loc) · 868 Bytes
/
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
// URL : https://leetcode.com/problems/game-of-life/
class Solution {
public:
void liveCheck(vector<vector<int>>& board, int r, int c) {
int cnt = 0;
for (int i = r - 1; i <= r + 1; i++) {
if (i < 0 || i >= board.size()) continue;
for (int j = c - 1; j <= c + 1; j++) {
if (j < 0 || j >= board[0].size() || (i == r && j == c)) continue;
if ( board[i][j] & 1 ) cnt++;
}
}
if ( board[r][c] == 1 && (cnt == 2 || cnt == 3) ) board[r][c] = 3;
if ( board[r][c] == 0 && cnt == 3 ) board[r][c] = 2;
}
void gameOfLife(vector<vector<int>>& board) {
for (int i = 0; i < board.size(); i++) {
for (int j = 0; j < board[0].size(); j++) {
liveCheck(board, i, j);
}
}
for (int i = 0; i < board.size(); i++) {
for (int j = 0; j < board[0].size(); j++) {
board[i][j] >>= 1;
}
}
}
};