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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| package questions;
public class _289生命游戏 {
public void gameOfLife_mutiple_status(int[][] board) { int n = board.length, m = board[0].length; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { board[i][j] = getStatus(board, i, j); } }
int cur = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cur = board[i][j]; if (cur < 0) { board[i][j] = 0; } else if (cur > 0) { board[i][j] = 1; } } } }
private int getStatus(int[][] board, int i, int j) { int n = board.length, m = board[0].length; int cnt = 0; if (i - 1 >= 0) { cnt += getOrigin(board[i - 1][j]); } if (i + 1 < n) { cnt += board[i + 1][j]; } if (j - 1 >= 0) { cnt += getOrigin(board[i][j - 1]); } if (j + 1 < m) { cnt += board[i][j + 1]; }
if (i - 1 >= 0 && j - 1 >= 0) { cnt += getOrigin(board[i - 1][j - 1]); } if (i + 1 < n && j - 1 >= 0) { cnt += getOrigin(board[i + 1][j - 1]); } if (i - 1 >= 0 && j + 1 < m) { cnt += getOrigin(board[i - 1][j + 1]); } if (i + 1 < n && j + 1 < m) { cnt += board[i + 1][j + 1] > 0 ? 1 : 0; }
if (cnt < 2 && board[i][j] > 0) { return -1; } if (cnt > 3 && board[i][j] > 0) { return -1; } if (cnt == 3 && board[i][j] == 0) { return 2; } return board[i][j]; }
private int getOrigin(int status) { switch (status) { case 1: return 1; case -1: return 1; case 2: return 0; default: return 0; } }
public void gameOfLife(int[][] board) { int n = board.length, m = board[0].length; int[][] copyBoard = new int[n][m]; for (int row = 0; row < n; row++) { for (int col = 0; col < m; col++) { copyBoard[row][col] = board[row][col]; } } int[][] dpVertical = new int[n][m]; dpVertical[0] = copyBoard[0]; for (int i = 1; i < n; i++) { for (int j = 0; j < m; j++) { dpVertical[i][j] = dpVertical[i - 1][j] + board[i][j]; } }
for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (isValid(dpVertical, copyBoard, i, j)) { board[i][j] = 1; } else { board[i][j] = 0; } } } }
private boolean isValid(int[][] dpVertical, int[][] board, int i, int j) { int count = 0; int n = board.length, m = board[0].length; int maxVidx = Math.min(i + 1, n - 1);
if (i - 1 >= 0) { count += board[i - 1][j]; } if (i + 1 < n) { count += board[i + 1][j]; } if (j - 1 >= 0) { count += dpVertical[maxVidx][j - 1] - (i - 2 < 0 ? 0 : dpVertical[i - 2][j - 1]); } if (j + 1 < m) { count += dpVertical[maxVidx][j + 1] - (i - 2 < 0 ? 0 : dpVertical[i - 2][j + 1]); } if (count == 3 || (count == 2 && board[i][j] == 1)) { return true; } return false; }
public static void main(String[] args) { _289生命游戏 gameOfLife = new _289生命游戏(); int[][] board = new int[][]{ {0, 1, 0}, {0, 0, 1}, {1, 1, 1}, {0, 0, 0}}; gameOfLife.gameOfLife_mutiple_status(board); for (int[] ints : board) { for (int anInt : ints) { System.out.print(anInt + " "); } System.out.println(); } } }
|