forked from aswinkumarrk/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameOfLife_289.java
More file actions
72 lines (66 loc) · 2.5 KB
/
GameOfLife_289.java
File metadata and controls
72 lines (66 loc) · 2.5 KB
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
package com.leetcode.problems.medium;
import com.util.LogUtil;
/**
* @author neeraj on 12/10/19
* Copyright (c) 2019, data-structures.
* All rights reserved.
*/
public class GameOfLife_289 {
static int[][] neighbours = new int[][]{{-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}};
static int liveStatus = 2;
static int dieStatus = 3;
public static void main(String[] args) {
gameOfLife(new int[][]{
{0, 1, 0},
{0, 0, 1},
{1, 1, 1},
{0, 0, 0}
});
}
public static void gameOfLife(int[][] board) {
LogUtil.printMultiDimensionArray(board);
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
// We always calculate the liveNeighbours from the original state of board, as requested in Question
// Not what happened after any modifications.
int liveNeighbours = getLiveNeighbours(board, i, j);
if (board[i][j] == 0 && liveNeighbours == 3) {
board[i][j] = liveStatus;
}
if (board[i][j] == 1) {
if (liveNeighbours < 2 || liveNeighbours > 3) {
board[i][j] = dieStatus;
}
}
}
}
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == liveStatus) {
board[i][j] = 1;
}
if (board[i][j] == dieStatus) {
board[i][j] = 0;
}
}
}
LogUtil.printMultiDimensionArray(board);
}
private static int getLiveNeighbours(int[][] board, int i, int j) {
int count = 0;
for (int k = 0; k < neighbours.length; k++) {
int[] move = neighbours[k];
int tempI = i + move[0];
int tempJ = j + move[1];
// Is Safe to Move
if (tempI >= 0 && tempJ >= 0 && tempI < board.length && tempJ < board[0].length) {
// Why are we checking the dieStatus because, due to neighbours, the cell tempI,tempJ must
// have died, but we always want to find live neighbours based on original state of the board.
if (board[tempI][tempJ] == 1 || board[tempI][tempJ] == dieStatus) {
count++;
}
}
}
return count;
}
}