关闭

Middle-题目34:289. Game of Life

131人阅读 评论(0) 收藏 举报
分类:

题目原文:
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中还有一个神算法,有待研读。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:64684次
    • 积分:3289
    • 等级:
    • 排名:第10725名
    • 原创:270篇
    • 转载:53篇
    • 译文:0篇
    • 评论:8条
    文章分类
    最新评论