-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameOfLife.java
110 lines (88 loc) · 2.99 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.gkonovalov.problems.arrays.matrix;
/**
* Created by Georgiy Konovalov on 14/08/2023.
* <p>
* @see <a href="https://leetcode.com/problems/game-of-life/">
* [Medium][289] - Game of Life
* </a>
* </p>
* Runtime Complexity: O(mn) for {@code gameOfLife}.
* Space Complexity: O(1) for {@code gameOfLife}.
*/
public class GameOfLife {
public static void main(String[] args) {
int[][] arr = {
{1, 0},
{0, 1}
};
gameOfLife(arr);
}
public static void gameOfLife(int[][] board) {
int n = board.length;
int m = board[0].length;
final int deadWasLive = 2;
final int liveWasDead = 3;
int[][] pos = {{0, 1}, {1, 0}, {1, 1}, {-1, -1}, {-1, 0}, {0, -1}, {1, -1}, {-1, 1}};
for (int row = 0; row < n; row++) {
for (int col = 0; col < m; col++) {
int cell = board[row][col];
int live = 0;
for (int[] p : pos) {
int r = row + p[0];
int c = col + p[1];
if (isValid(board, r, c)) {
switch (board[r][c]) {
case 1:
case deadWasLive:
live++;
break;
}
}
}
if (cell == 1 && (live < 2 || live > 3)) {
board[row][col] = deadWasLive;
} else if (cell == 0 && live == 3) {
board[row][col] = liveWasDead;
}
}
}
for (int row = 0; row < n; row++) {
for (int col = 0; col < m; col++) {
board[row][col] %= 2;
}
}
}
public static void gameOfLife2(int[][] board) {
int n = board.length;
int m = board[0].length;
int[][] copy = new int[n][m];
for (int i = 0; i < n; i++) {
copy[i] = board[i].clone();
}
int[][] pos = {{0, 1}, {1, 0}, {1, 1}, {-1, -1}, {-1, 0}, {0, -1}, {1, -1}, {-1, 1}};
for (int row = 0; row < n; row++) {
for (int col = 0; col < m; col++) {
int cell = copy[row][col];
int live = 0;
for (int[] p : pos) {
int r = row + p[0];
int c = col + p[1];
if (isValid(copy, r, c) && copy[r][c] == 1) {
live++;
}
}
if (cell == 1 && (live < 2 || live > 3)) {
board[row][col] = 0;
} else if (cell == 0 && live == 3) {
board[row][col] = 1;
}
}
}
}
private static boolean isValid(int[][] board, int row, int col) {
if (row < 0 || col < 0 || row >= board.length || col >= board[row].length) {
return false;
}
return true;
}
}