Game of Life

来源:互联网 发布:简单网站编程教程 编辑:程序博客网 时间:2024/05/18 01:08

题目名称
Merge Sorted Array—LeetCode链接

描述
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.

Follow up:

  1. 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.
  2. 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表示细胞是活的。除了边缘细胞,每个细胞都有8个相邻细胞,在下一次状态更新的时候有几个规则:

  1. 活细胞周围的活细胞数目小于2,则下次死亡;
  2. 活细胞周围有2个或者3个活细胞,则下次继续存活;
  3. 活细胞周围有大于3个活细胞,则下次死亡;
  4. 死细胞周围正好有3个活细胞,则下次重生。

  题目中提醒了要考虑最外层的细胞,因为最外层的细胞并没有8个相邻细胞,解决这个问题的办法是对原来细胞进行扩充,在原来m×n的board上增加一个最外层,最外层的细胞状态全为死亡(0)。则原来的细胞board中每个细胞都有8个相邻元素了。

C++代码

/**************************************************************Copyright:武汉大学计算机学院B507Author: RyanDate:2015-11-2Description:Game of Life**************************************************************/#include<iostream>#include<vector>using namespace std;int live(int i,int j,vector< vector<int> >& matrix);void gameOfLife(vector< vector<int> >& board) {    int m=board.size(),n=board[0].size();    if(m==0 || n==0)        return;    vector< vector<int> > temp(m,vector<int>(n));    //对原board进行扩展,增加最外圈,假设做外圈所有细胞都是死亡的,状态为0     for(int i=0;i<m;i++){        board[i].push_back(0);        board[i].insert(board[i].begin(),0);    }    board.push_back(vector<int>(n+2));    board.insert(board.begin(),vector<int>(n+2));    for(int j=1;j<m+1;j++) {        for(int k=1;k<n+1;k++) {            int count = live(j,k,board);            if(board[j][k]==1){                if(count<2)                    temp[j-1][k-1]=0;                if(count==2 || count==3)                    temp[j-1][k-1]=1;                if(count>3)                    temp[j-1][k-1]=0;            }            else if(board[j][k]==0){                if(count==3)                    temp[j-1][k-1]=1;            }        }    }     board = temp;}//用来计算相邻细胞中存活细胞的数目int live(int i,int j,vector< vector<int> >& matrix) {    int count = 0;    if(matrix[i-1][j-1])        count++;    if(matrix[i-1][j])        count++;    if(matrix[i-1][j+1])        count++;    if(matrix[i][j-1])        count++;    if(matrix[i][j+1])        count++;    if(matrix[i+1][j-1])        count++;    if(matrix[i+1][j])        count++;    if(matrix[i+1][j+1])        count++;    return count;}//测试用例int main() {    int a[]={1,0,0};    int b[]={0,1,1};    int c[]={1,0,1};    vector<int> v1(a,a+3);    vector<int> v2(b,b+3);    vector<int> v3(c,c+3);    vector< vector<int> > board;    board.push_back(v1);    board.push_back(v2);    board.push_back(v3);    gameOfLife(board);    for(int i=0;i<board.size();i++){        cout<<"["<<" ";        for(int j=0;j<board[0].size();j++){            cout<<board[i][j]<<" ";        }        cout<<"]"<<endl;    }    return 0;}

运行结果如下:

这里写图片描述

总结
  还是二维向量问题,最近做的比较多,难度不大。

1 0
原创粉丝点击