LeetCode 289. Game of Life

来源:互联网 发布:淘宝店铺名推荐 编辑:程序博客网 时间:2024/06/07 08:53

问题描述

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:

  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?

问题分析

给定一个二维数组,这个数组中的数被标识为0,和1.其中0表示死 。1表示生。其中如果当前节点是生。
如果当前存活的
* 如果存活细胞小于2,那么这个细胞下一轮是死的。 01
* 如果存活的细胞是2到3个,下一轮状态 存活 11
* 如果存活的细胞以3个以上, 则是死亡 01
* 如果当前的细胞是死细胞,如果周围存活的细胞是3个 活。 10
这个题有额外的需求是不能使用额外的空间,这个题就使用 当前数组中的位置来表示当前的值。技能表示当前位置的值,也能表示下一轮计算后的值。其中低位表示当前这轮的值,高位表示下一轮的值。通过 %取得当前这一轮的值。/表示下一轮的值。
这个题描述比较复杂,实现并不复杂。

代码实现

/**     * 给定一个二维数组,其中 0表示死亡,1表示存活。     * 不能使用额外的空间这个时候就需要在每一位中技能记录当前位的值,也能记录下一轮的值。     * 其中每一个数周围最多有8个数     * 如果当前存活的     * 如果存活细胞小于2,那么这个细胞下一轮是死的。 01     * 如果存活的细胞是2到3个,下一轮状态  存活 11     * 如果存活的细胞以3个以上,  则是死亡   01     * <p>     * 如果当前的细胞是死细胞,如果周围存活的细胞是3个活。 10     *     * @param board     */    public void gameOfLife(int[][] board) {        for (int i = 0; i < board.length; i++) {            for (int j = 0; j < board[0].length; j++) {                int liveNum = getLiveCount(board, i, j);                if (board[i][j] == 0) {//当前死的                    if (liveNum == 3) {                        board[i][j] = board[i][j] + 10;                    }                } else {                    if (liveNum == 2 || liveNum == 3) {                        board[i][j] = board[i][j] + 10;                    }                }            }        }        for (int i = 0; i < board.length; i++) {            for (int j = 0; j < board[i].length; j++) {                board[i][j] = board[i][j] / 10;            }        }    }    /**     * 给定一个数组,求出这个数组中为0的数的个数     *     * @param board     * @param i     * @param j     * @return     */    private int getLiveCount(int[][] board, int i, int j) {        int count = 0;        for (int left = i - 1; left <= i + 1; left++) {            for (int right = j - 1; right <= j + 1; right++) {                if (left < 0 || left >= board.length || right < 0 || right >= board[i].length                        || (left == i && right == j)) {                    continue;                }                if (board[left][right] % 10 == 1) {                    count++;                }            }        }        return count;    }
原创粉丝点击