zoukankan      html  css  js  c++  java
  • 289. Game of Life

    题目:

    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?

    答案:

    生存游戏规则:

    mxn个细胞,1代表生存状态,0代表死亡状态,而细胞的下一状态与它周围细胞的当前状态有关,

          1.如果一个活细胞(等于1)周围的活细胞小于2个,它在下一状态将会死去(变成0)【1->0】

          2.如果一个活细胞周围活细胞为2个或3个,在下一状态它仍旧生存【1->1】

          3.如果一个活细胞周围活细胞大于3个,在下一状态它将会死去【1->0】

          4.如果一个死细胞周围活细胞为3个,在下一状态它将会复活【0->1】

    要求就就地解决这个问题,注意所有细胞的状态是同时更新的,不能用一个细胞更新过后的值去更新其他的细胞

    思路:

    在整个矩阵更新完毕前,我们不能丢掉当前每个细胞的值,所以我们可以把当前值放在低位,把更新过后的值放在高位,所以以上四种情况分别可表示为:

    01【十进制为1】

    11【十进制为3】

    01【十进制为1】

    10【十进制为2】

    然后可以进行一些二进制的计算,我们可以观察得到:

           以上数字&1可以得到当前状态

                      >>1可以得到下一状态

     1 class Solution {
     2 public:
     3     void gameOfLife(vector<vector<int>>& board) {
     4         int x=board.size();
     5         int y=board[0].size();
     6         for(int i=0;i<x;i++){
     7             for(int j=0;j<y;j++){
     8                 int neighbors = getNeighbors(board, i, j);
     9                 if(board[i][j]==1){
    10                     if(neighbors==2 || neighbors==3){
    11                         board[i][j]=3;
    12                     }
    13                 }
    14                 else if(board[i][j]==0){
    15                     if(neighbors==3){
    16                         board[i][j]=2;
    17                     }
    18                 }
    19             }
    20         }    
    21         for(int i=0;i<x;++i){
    22             for(int j=0;j<y;++j){
    23                 board[i][j]>>=1;
    24             }
    25         }
    26     }
    27     int getNeighbors(vector<vector<int>>& board,int m,int n){
    28         int c=0;
    29         for(int i=m-1;i<=m+1;i++){
    30             for(int j=n-1;j<=n+1;j++){
    31                 if(i>=0 && i<board.size() && j>=0 && j<board[0].size()){
    32                     c += board[i][j]&1;
    33                 }
    34             }
    35         }
    36         c-=board[m][n]&1;
    37         return c;
    38     }
    39 };
  • 相关阅读:
    java.lang.Math.pow方法
    【动态代理】增强代理方法、代理多种方法
    Exception in thread "main" java.net.ConnectException: Connection refused: connect
    serialVersionUID设置自动添加
    三个线程之间的通信
    LinkedList源码分析
    JavaScript学习
    Java学习大纲
    w3cschool-javascript教程-学习笔记
    FreeMarker学习总结
  • 原文地址:https://www.cnblogs.com/Reindeer/p/5656035.html
Copyright © 2011-2022 走看看