-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path289.cpp
More file actions
executable file
·39 lines (39 loc) · 1.86 KB
/
289.cpp
File metadata and controls
executable file
·39 lines (39 loc) · 1.86 KB
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
class Solution {
public:
void gameOfLife(vector<vector<int>>& board) {
for (int i = 0; i < board.size(); i++)
for (int j = 0; j < board[i].size(); j++){
int live = 0;
int die = 0;
if (i - 1 >= 0){
if (board[i-1][j] == 0) die ++; else live ++;
if (j - 1 >= 0) {
if (board[i-1][j-1] == 0) die ++; else live ++;
if (board[i][j-1] == 0) die ++; else live ++;
}
if (j + 1 < board[i].size()){
if (board[i-1][j+1] == 0) die ++; else live ++;
if (board[i][j+1] == 0) die ++; else live ++;
}
}
if (i + 1 < board.size()){
if (board[i+1][j] == 0) die ++; else live ++;
if (j - 1 >= 0) {if (board[i+1][j-1] == 0) die ++; else live ++;}
if (j + 1 < board[i].size()) {if (board[i+1][j+1] == 0) die ++; else live ++;}
}
cout << live << ' ' << die << endl;
if (board[i][j] == 1){
//Any live cell with fewer than two live neighbors dies, as if caused by under-population.
if (live < 2) board[i][j] = 0;
//Any live cell with two or three live neighbors lives on to the next generation.
if (live == 2 || live == 3) board[i][j] = 1;
//Any live cell with more than three live neighbors dies, as if by over-population..
if (live > 3) board[i][j] = 0;
}
else{
//Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
if (live == 3) board[i][j] = 1;
}
}
}
};