zoukankan      html  css  js  c++  java
  • [leetcode] 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):
    
    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?

    分析:

    方法一 :You cannot update some cells first and then use their updated values to update other cells,故可以考虑另开一个数组int[][] tmp,用来保存修改后的每个元素,之后再把tmp复制到数组board中。

    方法二:考虑下面的情况:

    1. board[i][j] == 1时,当 sum < 2 || sum >3时, cell状态由1变成0,我们令board[i][j] = -1;

    2. board[i][j] == 0时,当sum == 3时,cell状态由0变成1,我们令board[i][j] = -2;

    3. 其他情况下,cell状态无变化。

    当我们在计算sum时,如果board[a][b] == -2 || board[a][b] == -1,则返回 board[a][b] + 2;

    当我们在还原cell中的值时,如果board[i][j] = -1,则让board[i][j]=0;如果board[i][j] = -2,则让board[i][j] = 1.

    代码如下:

        public void gameOfLife(int[][] board) {
            for (int i = 0; i < board.length; i++) {
                for (int j = 0; j < board[0].length; j++) {
                    int sum = 0;
                    sum += checkValue(board, i, j-1)
                        +  checkValue(board, i, j+1)
                        +  checkValue(board, i-1, j)
                        +  checkValue(board, i+1, j)
                        +  checkValue(board, i-1, j-1)
                        +  checkValue(board, i-1, j+1)
                        +  checkValue(board, i+1, j-1)
                        +  checkValue(board, i+1, j+1);
                    if (board[i][j] == 1) {
                        System.out.println(sum);
                        if (sum < 2 || sum >3) {
                            board[i][j] = -1;
                        }
                    } else {
                        if (sum == 3) {
                            board[i][j] = -2;
                        }
                    }
                }
            }
            for (int i = 0; i < board.length; i++) {            //还原cell内的值
                for (int j = 0; j < board[0].length; j++) {
                    if (board[i][j] == -2) {
                        board[i][j] = 1;
                    }
                    if (board[i][j] == -1) {
                        board[i][j] = 0;
                    }
                }
            }
        }
        public int checkValue(int[][] board, int a, int b) {
            if (a >=0 && a < board.length && b >= 0 && b < board[0].length) {
                if (board[a][b] == -2 || board[a][b] == -1) {
                    return board[a][b] + 2;
                }
                return board[a][b];
            }
            return 0;
        }
  • 相关阅读:
    Activiti7工作流+SpringBoot
    他为何放弃580万年薪 拿月薪500元跟马云创业(眼光毒辣,半年的机会损失不算大,大不了再干会本行)
    周鸿祎在360新员工入职培训上的讲话(他们都是太聪明,把自己混失败了。大家一定要记住,混日子就是在糜费自己的时间。假设你不喜欢360,你一定要尽快换,尽快找到自己喜欢的事情)
    Apache多虚拟主机多版本PHP(5.3+5.6+N)共存运行配置全过程
    估值800亿的小米,宣布正式进军欧洲市场
    英特尔投资:7200万美元投资12家创新公司,包括3家中国公司(www.intelcapital.com)
    应用监控Metrics
    六大原则理
    PostgreSQL
    CAP原理和BASE思想
  • 原文地址:https://www.cnblogs.com/lasclocker/p/4929456.html
Copyright © 2011-2022 走看看