forked from super30admin/Array-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameoflife.cpp
72 lines (57 loc) · 1.77 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
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
// Time Complexity :o(m*n)
// Space Complexity :o(1)
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :
// maintaing the change in each state ,later decide to use 1->0==>2; 0->1==>3
// Your code here along with comments explaining your approach
class Solution {
public:
void gameOfLife(vector<vector<int>>& board) {
int m=board.size();
int n=board[0].size();
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
int count_of_live=countof(board,i,j);
if(board[i][j]==1)
{
if(count_of_live<2 || count_of_live>3)
board[i][j]=2;
}
else
{
if(count_of_live==3)
board[i][j]=3;
}
}
}
// 1->0==>2
// 0->1==>3
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(board[i][j]==2)
board[i][j]=0;
else if(board[i][j]==3)
board[i][j]=1;
}
}
return;
}
// function to check surroundings of each element
int countof(vector<vector<int>>&board,int row,int col)
{
vector<vector<int>> matrix_neighbour_search{{-1,0}, {1,0}, {0,-1}, {0,1}, {-1,1}, {1,-1}, {-1,-1}, {1,1}};
int count=0;
for(int i=0;i<matrix_neighbour_search.size();i++)
{
int n_row=row+matrix_neighbour_search[i][0];
int n_col=col+matrix_neighbour_search[i][1];
if(n_row<=board.size()-1 && n_row>=0 && n_col<=board[0].size()-1 && n_col>=0 && (board[n_row][n_col]==2 || board[n_row][n_col]==1 ))
count++;
}
return count;
}
};