[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;
    }
原文地址:https://www.cnblogs.com/lasclocker/p/4929456.html