Middle-题目34:289. Game of Life

原创 2016年05月31日 15:56:35

题目原文:
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):
1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.
2. Any live cell with two or three live neighbors lives on to the next generation.
3. Any live cell with more than three live neighbors dies, as if by over-population..
4. 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.
题目大意:
给一个m*n的网格,每个网格中有一个细胞,它只有生(1)或死(0)两种状态。每个细胞有8个邻居(水平,垂直,对角线),每经过一个时间周期,按如下规则演化:
1. 若一个活着的细胞周围有少于2个活着的邻居,则它会死于孤独。
2. 若一个活着的细胞周围有2,3个活着的邻居,则它会继续活下去。
3. 若一个活着的细胞周围有3个以上活着的邻居,则它会死于人口过多。
4. 若一个死亡的细胞周围恰有3个活着的细胞,则它会复活,就好像繁殖。

给出一个网格当前的状态,求出下一个时间周期的状态。
题目分析:
直接模拟即可,在数邻居个数的时候要考虑一些边界情况(因为不一定都有8个邻居),数出来邻居之后按规则变化即可。
源码:(language:java)

public class Solution {
    private final int DEAD = 0;
    private final int ALIVE = 1;
    public void gameOfLife(int[][] board) {
        int m=board.length;
        int n=board[0].length;
        int[][] nextState = new int[m][n];
        for(int i=0;i<m;i++) {
            for(int j=0;j<n;j++) {
                //Rule 1:Any live cell with fewer than two live neighbors dies, as if caused by under-population.
                if(board[i][j] == ALIVE && getNeighbours(board,i,j) <= 1)  
                    nextState[i][j] = DEAD;
                //Rule 3:Any live cell with more than three live neighbors dies, as if by over-population.
                else if(board[i][j] == ALIVE && getNeighbours(board,i,j) > 3)  
                    nextState[i][j] = DEAD;
                //Rule 4:Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
                else if(board[i][j] == DEAD && getNeighbours(board,i,j) == 3)
                    nextState[i][j] = ALIVE;
                else
                    nextState[i][j]=board[i][j];
                //System.out.println("i="+i+",j="+j+",neighbours="+getNeighbours(board,i,j,m,n)+",this state="+board[i][j]+",nextState="+nextState[i][j]);
            }
        }
        for(int i=0;i<m;i++) {
            for(int j=0;j<n;j++) {
                board[i][j]=nextState[i][j];
            }
        }
    }
    private int getNeighbours(int[][] board,int i,int j) {
        return getState(board,i-1,j-1) + getState(board,i-1,j) + getState(board,i-1,j+1) + getState(board,i,j-1) + getState(board,i,j+1) + getState(board,i+1,j-1) + getState(board,i+1,j) + getState(board,i+1,j+1);
    }
    private int getState(int[][] board, int i, int j) {
        int m = board.length;
        int n = board[0].length;
        if(i<0 || j<0 || i==m || j==n)
            return 0;
        else 
            return board[i][j];
    }
}

成绩:
1ms,beats 13.28%,众数1ms,86.47%
Cmershen的碎碎念:
Discuss中还有一个神算法,有待研读。

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

[LeetCode 289] Game of Life

According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular...
  • sbitswc
  • sbitswc
  • 2015年10月12日 07:07
  • 1579

leetcode 289. Game of Life

According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular au...

Leetcode 289. Game of Life (Medium) (cpp)

Leetcode 289. Game of Life (Medium) (cpp)
  • Niko_Ke
  • Niko_Ke
  • 2016年07月31日 23:57
  • 185

Leetcode 289. Game of Life[medium]

题目: According to the Wikipedia’s article: “The Game of Life, also known simply as Life, is a cellul...

【leetcode】289. Game of Life

【leetcode】289. Game of Life

leetcode289 : Game of Life

1、原题如下: According to the Wikipedia’s article: “The Game of Life, also known simply as Life, is a ce...

Leetcode 73. Set Matrix Zeroes & 289. Game of Life

73. Set Matrix Zeroes Total Accepted: 77299 Total Submissions: 224251 Difficulty: Medium G...

LeetCode 289 Game of Life(生命游戏)(Array)

翻译根据维基百科的文章介绍:“Game of Life,简称为Life,是一个被英国数学家John Conway在1970年提出的细胞自动分裂器。”给定一个m x n的空间,每个细胞有一个初始状态li...
  • NoMasp
  • NoMasp
  • 2016年08月04日 20:55
  • 1996

[LeetCode]289. Game of Life

MediumAccording to the Wikipedia’s article: “The Game of Life, also known simply as Life, is a cellu...

[leetcode]289. Game of Life (java)

题目: According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a ce...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Middle-题目34:289. Game of Life
举报原因:
原因补充:

(最多只允许输入30个字)