-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameOfLife.java
87 lines (78 loc) · 1.87 KB
/
GameOfLife.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
package matrix;
/**
* @author Shogo Akiyama
* Solved on 12/18/2019
*
* 289. Game of Life
* https://leetcode.com/problems/game-of-life/
* Difficulty: Medium
*
* Approach: In-place (Optimized)
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Game of Life.
* Memory Usage: 34.5 MB, less than 100.00% of Java online submissions for Game of Life.
*
* Time Complexity: O(m*n)
* Space Complexity: O(1)
* Where m * n is the number of elements in a matrix
*
* @see MatrixTest#testGameOfLife()
*/
public class GameOfLife {
private int[][] grid;
private int m;
private int n;
private int[][] directions = new int[][] {
{ -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 }, // horizontal and vertical
{ -1, -1 }, { -1, 1 }, { 1, -1 }, { 1, 1 } // diagonal
};
public void gameOfLife(int[][] board) {
if (board == null || board.length == 0) {
return;
}
grid = board;
m = board.length;
n = board[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
signal(i, j);
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
update(i, j);
}
}
}
// each alive cell sends signals to adjacent nodes by adding 10 to their values
private void signal(int y, int x) {
if (grid[y][x] % 10 == 1) {
for (int[] dir : directions) {
int ny = y + dir[0];
int nx = x + dir[1];
if (ny >= 0 && ny < m && nx >= 0 && nx < n) {
grid[ny][nx] += 10;
}
}
}
}
// each cell counts the number of signals received (grid[y][x]/10)
// then decide if they will be alive or dead
private void update(int y, int x) {
int neighbors = grid[y][x] / 10;
if (grid[y][x] % 10 == 1) {
if (neighbors < 2) {
grid[y][x] = 0;
} else if (neighbors < 4) {
grid[y][x] = 1;
} else {
grid[y][x] = 0;
}
} else {
if (neighbors == 3) {
grid[y][x] = 1;
} else {
grid[y][x] = 0;
}
}
}
}