-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame Of Life.cpp
47 lines (46 loc) · 1.51 KB
/
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
class Solution {
public:
void gameOfLife(vector<vector<int>>& board) {
vector<vector<int>> temp(board.size(), vector<int>(board[0].size(), 0));
for(int i = 0; i < board.size(); i++){
for(int j = 0; j < board[0].size(); j++){
if(board[i][j]){
if(i-1 >= 0 && j-1 >= 0){
temp[i-1][j-1]++;
}
if(i-1 >= 0){
temp[i-1][j]++;
}
if(i-1 >= 0 && j+1 < board[0].size()){
temp[i-1][j+1]++;
}
if(j-1 >= 0){
temp[i][j-1]++;
}
if(j+1 < board[0].size()){
temp[i][j+1]++;
}
if(i+1 < board.size()){
if(j-1 >= 0){
temp[i+1][j-1]++;
}
temp[i+1][j]++;
if(j+1 < board[0].size()){
temp[i+1][j+1]++;
}
}
}
}
}
for(int i = 0; i < board.size(); i++){
for(int j = 0; j < board[0].size(); j++){
if(temp[i][j] < 2 || temp[i][j] > 3){
board[i][j] = 0;
}
else if(temp[i][j] == 3){
board[i][j] = 1;
}
}
}
}
};