According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."
Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):
- Any live cell with fewer than two live neighbors dies, as if caused by under-population.
- Any live cell with two or three live neighbors lives on to the next generation.
- Any live cell with more than three live neighbors dies, as if by over-population..
- Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
Write a function to compute the next state (after one update) of the board given its current state.
Follow up:
- Could you solve it in-place? Remember that the board needs to be updated at the same time: You cannot update some cells first and then use their updated values to update other cells.
- In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches the border of the array. How would you address these problems?
每个细胞有初始状态0(死)和1(活),先定义一组中间状态,中间状态要求能看出一个细胞变化前后的生死情况。
Run time complexity is O(n ^ 2), constant space.
public class Solution { final int dead = 0; final int alive = 1; final int deadToDead = 0; // 死->死 final int aliveToAlive = 1; // 活->活 final int aliveToDead = 2; // 活->死 final int deadToAlive = 3; // 死->活 public void gameOfLife(int[][] board) { if (board == null) { return; } int row = board.length; int col = board[0].length; if (row == 0 || col == 0) { return; } int count = 0; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { count = 0; // 统计周边生命情况 if (i > 0 && j > 0 && isAliveOld(board[i - 1][j - 1])) { count++; } if (i > 0 && isAliveOld(board[i - 1][j])) { count++; } if (i > 0 && j < col - 1 && isAliveOld(board[i - 1][j + 1])) { count++; } if (j > 0 && isAliveOld(board[i][j - 1])) { count++; } if (j < col - 1 && isAliveOld(board[i][j + 1])) { count++; } if (i < row - 1 && j > 0 && isAliveOld(board[i + 1][j - 1])) { count++; } if (i < row - 1 && isAliveOld(board[i + 1][j])) { count++; } if (i < row - 1 && j < col - 1 && isAliveOld(board[i + 1][j + 1])) { count++; } // 判断当前细胞生病变化 if (board[i][j] == alive) { if (count < 2) { // live cell with fewer than 2 live neighbors dies, under-population board[i][j] = aliveToDead; } else if (count == 2 || count == 3) { // live cell with 2 or 3 live neighbors lives on to the next generation board[i][j] = aliveToAlive; } else { // live cell with more than 3 live neighbors dies, over-population board[i][j] = aliveToDead; } } else { if (count == 3) { // dead cell with exactly 3 live neighbors becomes a live cell, reproduction board[i][j] = deadToAlive; } else { board[i][j] = deadToDead; } } } } // 根据变化后状态重新赋值 for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (isAliveNew(board[i][j])) { board[i][j] = alive; } else { board[i][j] = dead; } } } } // 判断某个细胞在变化前是否存活 public boolean isAliveOld(int cell) { if (cell == aliveToAlive || cell == aliveToDead) { return true; } return false; } // 判断某个细胞在变化后是否存活 public boolean isAliveNew(int cell) { if (cell % 2 == 1) { // 状态1和3 return true; } return false; } }
No comments:
Post a Comment