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

用 React + es6 完成一个著名的生命游戏(Game of life,Conway)

http://elevenbeans.github.io/2017/04/05/Game-of-life/ 这是有名的康威生命游戏, 描述的是一种细胞自动机。 对一个 M*N 的区域,每一...
  • sinat_17775997
  • sinat_17775997
  • 2017年04月14日 12:25
  • 1375

生命游戏简介及算法分析(整理)

生命游戏 生命游戏(game of life)为1970年由英国数学家J. H. Conway所提出,某一细胞的邻居包括上、下、左、右、左上、左下、右上与右下相邻之细胞。 规则 生命游戏中,对于任意细...
  • u011439689
  • u011439689
  • 2013年12月09日 17:20
  • 4514

HDU 2053 Switch Game(开灯问题,唯一分解定理)

Switch Game Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To...
  • hurmishine
  • hurmishine
  • 2016年05月08日 19:23
  • 2057

【leetcode】289. Game of Life

【leetcode】289. Game of Life
  • chenyj92
  • chenyj92
  • 2016年04月28日 21:49
  • 485

[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
  • 1622

leetcode 289. Game of Life

According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular au...
  • u014568921
  • u014568921
  • 2016年02月29日 12:08
  • 271

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

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

Leetcode 289. Game of Life[medium]

题目: According to the Wikipedia’s article: “The Game of Life, also known simply as Life, is a cellul...
  • qq379548839
  • qq379548839
  • 2016年10月26日 19:50
  • 109

leetcode题解-289. Game of Life

题目:According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular...
  • liuchonge
  • liuchonge
  • 2017年04月19日 15:30
  • 569

leetcode-java-289. Game of Life

/* According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular...
  • github_34514750
  • github_34514750
  • 2016年09月04日 12:47
  • 119
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Middle-题目34:289. Game of Life
举报原因:
原因补充:

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