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;
        }
  • 相关阅读:
    抓取到的网页写入数据库后是乱码的解决方法
    关于SecureCRT v7.0.2.418 安装、破解和修改
    负数的除法和取模运算(Python 2.7和C的比较)
    单模式匹配匹配算法
    Python解析网页工具:PyQuery
    asp.net json,对象,字符串的相互转换
    asp.net 后台 get,post请求
    查看局域网内所有IP
    MSSql性能优化
    js中对象复制以及apply方法的使用
  • 原文地址:https://www.cnblogs.com/lasclocker/p/4929456.html
Copyright © 2011-2022 走看看