-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLifeGame.java
143 lines (130 loc) · 5.1 KB
/
LifeGame.java
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*https://leetcode.com/problems/game-of-life/*/
/*Pratik*/
class Solution {
public void gameOfLife(int[][] board)
{
if(board==null || board.length==0 || board[0].length==0)return;
for(int i=0;i<board.length;i++)
{
for(int j=0;j<board[0].length;j++)
{
int count = 0;
if(isAlive(board,i-1,j))count++;
if(isAlive(board,i,j-1))count++;
if(isAlive(board,i-1,j-1))count++;
if(isAlive(board,i-1,j+1))count++;
if(isAlive(board,i+1,j))count++;
if(isAlive(board,i+1,j-1))count++;
if(isAlive(board,i+1,j+1))count++;
if(isAlive(board,i,j+1))count++;
if(board[i][j]==1)
{
if(count>3 || count<2)
{
board[i][j] = 3;
}
else if(count==2 || count==3)
{
board[i][j] = 1;
}
}
else
{
if(count==3)
{
if(board[i][j]==0)board[i][j] = 2;
}
}
}
}
for(int i=0;i<board.length;i++)
{
for(int j=0;j<board[0].length;j++)
{
if(board[i][j]==3)board[i][j] = 0;
else if(board[i][j]==2)board[i][j] = 1;
}
}
}
public boolean isAlive(int[][] board,int i,int j)
{
if(i<0 || j<0 || i>=board.length || j>=board[0].length)
{
return false;
}
if(board[i][j]==1|| board[i][j]==3)return true;
return false;
}
}
/*Prateekshya*/
/*Brute Force*/
class Solution {
public void gameOfLife(int[][] board) {
int[][] result = new int[board.length][board[0].length];
for (int i = 0; i < board.length; ++i)
{
for (int j = 0; j < board[0].length; ++j)
{
int neighbourCount = 0;
if (i > 0 && board[i-1][j] == 1) ++neighbourCount;
if (i < board.length-1 && board[i+1][j] == 1) ++neighbourCount;
if (j > 0 && board[i][j-1] == 1) ++neighbourCount;
if (j < board[0].length-1 && board[i][j+1] == 1) ++neighbourCount;
if (i > 0 && j > 0 && board[i-1][j-1] == 1) ++neighbourCount;
if (i > 0 && j < board[0].length-1 && board[i-1][j+1] == 1) ++neighbourCount;
if (i < board.length-1 && j > 0 && board[i+1][j-1] == 1) ++neighbourCount;
if (i < board.length-1 && j < board[0].length-1 && board[i+1][j+1] == 1) ++neighbourCount;
if (neighbourCount < 2) result[i][j] = 0;
else if (neighbourCount == 3) result[i][j] = 1;
else if (neighbourCount == 2 && board[i][j] == 1) result[i][j] = 1;
else if (neighbourCount > 3) result[i][j] = 0;
}
}
for (int i = 0; i < board.length; ++i)
for (int j = 0; j < board[0].length; ++j)
board[i][j] = result[i][j];
}
}
/*In place solution*/
/*
1->0 = -2
0->1 = -1
*/
class Solution {
public void gameOfLife(int[][] board) {
for (int i = 0; i < board.length; ++i)
{
for (int j = 0; j < board[0].length; ++j)
{
int neighbourCount = 0;
if (i > 0 && (board[i-1][j] == 1 || board[i-1][j] == -2)) ++neighbourCount;
if (i < board.length-1 && (board[i+1][j] == 1 || board[i+1][j] == -2)) ++neighbourCount;
if (j > 0 && (board[i][j-1] == 1 || board[i][j-1] == -2)) ++neighbourCount;
if (j < board[0].length-1 && (board[i][j+1] == 1 || board[i][j+1] == -2)) ++neighbourCount;
if (i > 0 && j > 0 && (board[i-1][j-1] == 1 || board[i-1][j-1] == -2)) ++neighbourCount;
if (i > 0 && j < board[0].length-1 && (board[i-1][j+1] == 1 || board[i-1][j+1] == -2)) ++neighbourCount;
if (i < board.length-1 && j > 0 && (board[i+1][j-1] == 1 || board[i+1][j-1] == -2)) ++neighbourCount;
if (i < board.length-1 && j < board[0].length-1 && (board[i+1][j+1] == 1 || board[i+1][j+1] == -2)) ++neighbourCount;
if (neighbourCount < 2)
{
if (board[i][j] == 1)
board[i][j] = -2;
}
else if (neighbourCount == 3)
{
if (board[i][j] == 0)
board[i][j] = -1;
}
else if (neighbourCount > 3)
{
if (board[i][j] == 1)
board[i][j] = -2;
}
}
}
for (int i = 0; i < board.length; ++i)
for (int j = 0; j < board[0].length; ++j)
board[i][j] = board[i][j] == -2 ? 0 : (
board[i][j] == -1 ? 1 : board[i][j]);
}
}