LeetCode 289 Game of Life(生命游戏)(Array)

来源:互联网 发布:淘宝代办签证靠谱吗 编辑:程序博客网 时间:2024/05/20 20:04

翻译

根据维基百科的文章介绍:“Game of Life,简称为Life,是一个被英国数学家John Conway在1970年提出的细胞自动分裂器。”

给定一个m x n的空间,每个细胞有一个初始状态live(1)或dead(0)。每个细胞通过下面4种方式和周围的8个邻居交互(垂直、水平、交叉):

1,当前细胞为存活状态时,当周围低于2个(不包含2个)存活细胞时, 该细胞变成死亡状态。(模拟生命数量稀少)
2,当前细胞为存活状态时,当周围有2个或3个存活细胞时, 该细胞保持原样。
3,当前细胞为存活状态时,当周围有3个以上的存活细胞时,该细胞变成死亡状态。(模生命数量过多)
4,当前细胞为死亡状态时,当周围有3个存活细胞时,该细胞变成存活状态。 (模拟繁殖)

写一个函数用于计算空间的给定状态的下一个状态(当某个更新后)。

跟进:
1,你可以就地解决吗?记住这空间需要同时更新:你不能只首先更新一部分,然后用这些新的状态去更新别的细胞。
2,在这个问题,我们使用了二维数组。原则上,这个空间是无限大的,当这些变化影响到边界时,你是如何解决的?

原文

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?

分析

好久没有写算法了,都不习惯了。所以也就没解出来,参考了网上的答案,但还是说说分析过程吧。

细胞变为“活”,只有两种可能。当它本来为存活态时,邻居有2或3个时保持原来的状态,也就是“活”。还有一种是,当原本为死亡态时,邻居有3个时,也会变为“活”。

其余状态,要么是由存活态变为死亡态,要么就是保持死亡态。

我们通过加上10来保存这个细胞的状态,最后统一进行刷新,这也就是这个问题的核心。

if (count == 3 || (count == 2 && board[r][c] == 1)) board[r][c] += 10;

但因为是数组,所以也有边界。以后再用可变的数组修改吧,挺有意思的题目。两年前就听说了康威生命游戏,大家可以去看看它的Wiki。

代码

C Plus Plus

class Solution {public:    void gameOfLife(vector<vector<int>>& board) {        for (int r = 0; r < board.size() ; r++) {            for (int c = 0; c < board[r].size(); c++) {                int count = countNeighbors(board, r, c);                if (count == 3 || (count == 2 && board[r][c] == 1)) board[r][c] += 10;            }         }         flashBoard(board);    }    int countNeighbors(const vector<vector<int>>& board, int r, int c) {         int count = 0;         for (int row  = max(r - 1, 0) ; row <= min(r + 1, (int) board.size() - 1); row ++) {              for (int col = max(c - 1, 0); col <= min(c + 1, (int) board[row].size() - 1); col ++) {                 if (row != r || col != c) count += board[row][col] % 10;              }         }         return count;    }    void flashBoard(vector<vector<int>>& board) {         for (int i = 0; i < board.size(); i++) {             for (int j = 0; j < board[i].size(); j++) {                 board[i][j] = board[i][j] / 10;             }        }    }};

Java

updated at 2016/09/04
    public void gameOfLife(int[][] board) {        for (int r = 0; r < board.length; r++) {            for (int c = 0; c <board[r].length; c++) {                int count = countNeighbors(board, r, c);                if (count == 3 || (count == 2 && board[r][c] == 1))                    board[r][c] += 10;            }        }        flashBoard(board);    }    int countNeighbors(int[][] board, int r, int c) {        int count = 0;        for (int row = Math.max(r - 1, 0); row <= Math.min(r + 1, (int)board.length - 1); row++) {            for (int col = Math.max(c - 1, 0); col <= Math.min(c + 1, (int)board[row].length - 1); col++) {                if (row != r || col != c)                    count += board[row][col] % 10;            }        }        return count;    }    void flashBoard(int[][] board) {        for (int i = 0; i < board.length; i++) {            for (int j = 0; j < board[i].length; j++) {                board[i][j] = board[i][j] / 10;                System.out.println("b = " + board[i][j]);            }        }    }
1 0
原创粉丝点击