-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlt38.cpp
76 lines (72 loc) · 2.29 KB
/
lt38.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
class Solution {
public:
void gameOfLife(vector<vector<int>>& board) {
int ni=board.size();
int nj=board[0].size();
int livingcount=0;
vector<vector<int>> board2;
board2=board;
for(int i=0;i<ni;i++){
for(int j=0;j<nj;j++){
if(i-1>=0){
//top element check
if(board2[i-1][j]==1){
livingcount++;
}
if(j-1>=0){
//top left elementcheck
if(board2[i-1][j-1]==1){
livingcount++;
}
}
if(j+1<=nj-1){
//top right element check
if(board2[i-1][j+1]==1){
livingcount++;
}
}
}
if(i+1<=ni-1){
//bottom element check
if(board2[i+1][j]==1){
livingcount++;
}
if(j-1>=0){
//bottom left element check
if(board2[i+1][j-1]==1){
livingcount++;
}
}
if(j+1<=nj-1){
//bottom right element check
if(board2[i+1][j+1]==1){
livingcount++;
}
}
}
if(j-1>=0){
//left element check
if(board2[i][j-1]==1){
livingcount++;
}
}
if(j+1<=nj-1){
//right element check
if(board2[i][j+1]==1){
livingcount++;
}
}
if(livingcount<2){
board[i][j]=0;
}
else if(livingcount>3){
board[i][j]=0;
}
else if(board[i][j]==0 && livingcount>=3){
board[i][j]=1;
}
livingcount=0;
}
}
}
};