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. The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously.

    Example:

    Input: 
    [
      [0,1,0],
      [0,0,1],
      [1,1,1],
      [0,0,0]
    ]
    Output: 
    [
      [0,0,0],
      [1,0,1],
      [0,1,1],
      [0,1,0]
    ]
    

    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?

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    && (count == 3)要加括号扩起来

    [一句话思路]:

    用cur next两个二进制位和board[][]总数 来共同表示邻居的数量

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    1. 自己另外写的新函数要写外面啊,不要写里面了
    2. 统计数量的时候,默认都是0,只需要写next是1的复活情况,其他的不用写

    [二刷]:

    1. int函数记得要写return
    2. m*n行的时候,index只能=到n-1 比如3*3 只能到012

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    用cur next两个二进制位和board[][]总数 来共同表示邻居的数量

    [复杂度]:Time complexity: O(mn) Space complexity: O(1)

    [算法思想:迭代/递归/分治/贪心]:

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

     [是否头一次写此类driver funcion的代码] :

    class Solution {
        public void gameOfLife(int[][] board) {
            //initialization
            int m = board.length;
            int n = board[0].length;
                  
            //corner case
            if (board == null || m == 0 || n == 0) return;
            
            //discuss the alive situations
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    int c = countNeighbor(board, m, n, i, j);
                    if (board[i][j] == 0 && (c == 3)) board[i][j] = 2;
                    if (board[i][j] == 1 && (c == 2 || c == 3)) board[i][j] = 3;
                }
            }
            
            //remove the current state by >>
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    board[i][j] >>= 1;
                }
            }
        }
        
        public int countNeighbor(int[][] board, int m, int n, int i, int j) {
            //add count in 4 directions
            int count = 0;
            for (int x = Math.min(0, i - 1); x <= Math.min(m - 1, i + 1); x++) {
                for (int y = Math.min(0, j - 1); y <= Math.min(n - 1, j + 1); y++) {
                    count += board[x][y] & 1;
                }
            }
            
            //remove the current state
            count -= board[i][j] & 1;
            return count;
        }
    }
    View Code

     [潜台词] :

     

  • 相关阅读:
    aws s3文件上传设置accesskey、secretkey、sessiontoken
    HTTP的Referrer和Referrer Policy设置
    小技巧-mac修改finder菜单栏
    使用katalon自带Spy功能获取/验证控件Selector、XPath
    java的8种基础类型
    Mac-搭建Hadoop集群
    新公司入职56天后的面谈小结
    Java对字符串加密并返回星号※
    为什么要写设计文档
    在Linux上部署Web项目
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9405305.html
Copyright © 2011-2022 走看看