您的位置:首页 > Web前端

289. Game of Life

2016-05-11 09:52 232 查看
题目:

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?

题意:
生命游戏。给出一个m*n的二维数组,1代码活节点,0代表死节点。每个格子都有八个邻居。生命游戏遵循以下四条规则:

1、一个活的格子若只有一个活着没有邻居,在下一秒将因寂寞而死亡;

2、一个活的格子若有两个或者三个邻居,在下一秒将继续活着;

3、一个活的格子若有四个或者四个以上的邻居,在下一秒将因拥挤而死亡;

4、一个死的格子若有三个邻居,在下一秒将活过来。

思路:

对二维数组中的每个元素轮训,找出每个元素周围八个格子活着格子的数目,并判断下一秒是活着还是死亡。将每个格子下一秒的状态保存在当前格子的第二位中,当完成所有元素下一秒状态之后,同时更新所有元素状态。

代码:

c++版:

class Solution {
public:
    void gameOfLife(vector<vector<int>>& board) {
        
        int m = board.size();
        int n = m ? board[0].size() : 0;
        
        if(m==0 || n==0){
            return;
        }
        
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                int count = 0;
                
                for(int k=max(i-1, 0); k<min(i+2, m); ++k){
                    for(int g=max(j-1, 0); g<min(j+2, n); ++g){
                        count += board[k][g] & 1;
                    }
                }
                
                if(count==3 || (count-board[i][j])==3){
                    board[i][j] |= 2;
                }
            }
        }
        
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                board[i][j]>>=1;
            }
        }
    }
};
java版:

public class Solution {
    public void gameOfLife(int[][] board) {
        
        int m = board.length;
        int n = board[0].length;
        
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                int lives = liveNeighbors(board, m, n, i, j);
                
                if(board[i][j]==1 && lives>=2 && lives<=3){
                    board[i][j] = 3; 
                }
                
                if(board[i][j]==0 && lives==3){
                    board[i][j] = 2;
                }
            }
        }
        
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                board[i][j] >>= 1;
            }
        }
    }
    
    public int liveNeighbors(int[][] board, int m, int n, int i, int j){
        int lives = 0;
        for(int k=Math.max(i-1, 0); k<Math.min(i+2, m); ++k){
            for(int g=Math.max(j-1, 0); g<Math.min(j+2, n); ++g){
                lives += board[k][g] & 1;
            }
        }
        
        lives -= board[i][j] & 1;
        return lives;
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: