LeetCode- 289. Game of Life - 思路详解-C++

来源:互联网 发布:数据库视频教程哪个好 编辑:程序博客网 时间:2024/06/14 07:31

题目

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):
1,Any live cell with fewer than two live neighbors dies, as if caused by under-population.
2,Any live cell with two or three live neighbors lives on to the next generation.
3,Any live cell with more than three live neighbors dies, as if by over-population..
4,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?

翻译

假设有一个大小为m*n的板子,有m行,n列个细胞。每个细胞有一个初始的状态,死亡或者存活。每个细胞和它的邻居。垂直,水平以及对角线。互动规则如下:
1,当前细胞存活时,当周围低于2个存活细胞时,该细胞死亡。(模拟生物数量过少,导致死亡)
2,当前细胞存活时,当周围有2个~3个存活细胞时,该细胞保持原状态。
3,当前细胞存活时,当周围有3个以上存活细胞时,该细胞死亡。(模拟生物数量过大导致死亡)
3,当前细胞死亡时,当周围存在3个存活细胞时,该细胞编程存活状态。(生活繁殖)

写一个函数,根据当前的状态计算下一个状态。

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

思路

该题目就是按照规则去处理。最简单的方式就是从新开辟一个数组。然后将结果存到新的数组,最后返回。但是题目要求在就地解决问题,也就是说空间复杂度为O(1)。

那么我们分析,是不是可以使用原数组缓存结果。但又不影响后续计算。因为所有数组的值只有两个,1:存活,0:死亡。
那么我们就可以用二进制11,表示当前状态存活,下一个状态存活。
01:表示当前状态存活,下一个状态死亡
10:表示当前状态死亡,下一个状态存活。
00:当前状态死亡,下一个状态存活。

最后数组就变成了值为00~11的数组,然后将所有元素向右移动一位,就变成了下一个状态。

代码

class Solution {public:    void gameOfLife(vector<vector<int>>& board) {        int rows = board.size();        int cols = board[0].size();        for(int i = 0; i < rows; ++i){            for(int j = 0; j < cols; ++j){                int neighbors = getNeighbors(board,i,j);                //如果当前细胞是活的                if((board[i][j] & 1) == 1){                    //如果活细胞周围邻居有两道三个为活细胞,则下一代继续存活                    if(neighbors == 2  || neighbors == 3){                        board[i][j] = 3;                    }                    //如果是01,则在更新时,死掉                    //如果小于2个或者大于3个,都是导致死亡                }else{                    //如果当前细胞是死的,其相邻有三个或者的邻居。则其变成一个活细胞                    if(neighbors == 3){                        board[i][j] = 2;                    }                }            }        }        for(int i = 0; i < rows; ++i){            for(int j = 0; j < cols;++j){                board[i][j] = board[i][j] >> 1;            }        }    }    //统计为1的邻居个数    int getNeighbors(vector<vector<int> > &board,int r,int c){        int rows = board.size();        int cols = board[0].size();        int res = 0;        for(int i = std::max(0,r-1); i <= std::min(rows-1,r+1);++i){            for(int j = std::max(0,c-1); j <= std::min(cols-1,c+1); ++j){                res += board[i][j]&1;            }        }        res -= board[r][c]&1;        return res;    }};
0 0
原创粉丝点击