-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameOfLife.cpp
45 lines (45 loc) · 1.2 KB
/
GameOfLife.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
class Solution {
public:
bool valid(int x,int y,vector<vector<int>>& board)
{
int n=board.size(),m=board[0].size();
if((x>=0)&&(x<n)&&(y>=0)&&(y<m)&&(board[x][y]&1))
return true;
return false;
}
void gameOfLife(vector<vector<int>>& board)
{
vector<vector<int>>moves = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,1},{1,-1},{-1,-1}};
int n=board.size();
if(n==0)
return;
int i,j,x,y,m=board[0].size(),lives,k;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
lives=0;
for(k=0;k<8;k++)
{
x=i+moves[k][0];
y=j+moves[k][1];
if(valid(x,y,board))
lives++;
}
if(board[i][j]&1)
{
if((lives>=2)&&(lives<=3))
board[i][j]=3;
}
else
{
if(lives==3)
board[i][j]=2;
}
}
}
for(i=0;i<n;i++)
for(j=0;j<m;j++)
board[i][j]/=2;
}
};