https://leetcode.com/problems/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):

  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?
struct neighbor{
int dies, lives;
neighbor() {dies = ; lives = ;}
neighbor(int d, int l): dies(d), lives(l) {}
};
class Solution {
public:
bool check(int m, int n, int a, int b) {
if(a< || a>=m || b< || b>=n) return false;
return true;
}
void gameOfLife(vector<vector<int>>& board) {
if(board.size() == ) return; int m = board.size(), n = board[].size(), ni, nj;
int dir[][] = {{-,},{-,-},{-,},{,-},{,},{,},{,-},{,}};
vector<vector<int> > res(m, vector<int>(n));
neighbor neg[m][n]; for(int i=;i<m;++i) {
for(int j=;j<n;++j) {
for(int k=;k<;++k) {
ni = i + dir[k][]; nj = j + dir[k][];
if(check(m, n, ni, nj)) {
if(board[ni][nj] == ) ++neg[i][j].lives;
else ++neg[i][j].dies;
}
}
}
} for(int i=;i<m;++i) {
for(int j=;j<n;++j) {
if(board[i][j] && neg[i][j].lives < ) res[i][j] = ;
else if(board[i][j] && (neg[i][j].lives == || neg[i][j].lives == )) res[i][j] = ;
else if(board[i][j] && neg[i][j].lives > ) res[i][j] = ;
else if(!board[i][j] && neg[i][j].lives == ) res[i][j] = ;
else res[i][j] = board[i][j];
}
} board = res;
}
};

leetcode 289: Game of Life

leetcode@ [289] Game of Life (Array)的更多相关文章

  1. LeetCode:Search in Rotated Sorted Array I II

    LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to y ...

  2. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

  3. LeetCode: Search in Rotated Sorted Array II 解题报告

    Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...

  4. [LeetCode] 289. Game of Life 生命游戏

    According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...

  5. [LeetCode] Minimum Moves to Equal Array Elements II 最少移动次数使数组元素相等之二

    Given a non-empty integer array, find the minimum number of moves required to make all array element ...

  6. [LeetCode] Minimum Moves to Equal Array Elements 最少移动次数使数组元素相等

    Given a non-empty integer array of size n, find the minimum number of moves required to make all arr ...

  7. [LeetCode] Two Sum II - Input array is sorted 两数之和之二 - 输入数组有序

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  8. [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  9. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

随机推荐

  1. PHP中CURL方法curl_setopt()函数的一些参数 (转)

    bool curl_setopt (int ch, string option, mixed value) curl_setopt()函数将为一个CURL会话设置选项.option参数是你想要的设置, ...

  2. <span> <div> 局部 keydown ,keyup事件。页面部分div $(document) 无效,可能焦点,添加焦点。

    前天改一个bug, js 实现的一个 面板拖拉,左右各两个列表,中间面板画线连接,页面左侧列表选中后,key 事件无效.右侧选中确有效,很奇怪,查看源码,左侧选中后,$(document).on(&q ...

  3. redisb并发访问慢出现的问题

    最近项目一上线,就问题颇多,本地测试,ok,上线后,大用户量的时候,顶不住.用了一个礼拜的时间发现的问题,总结下来. 项目是netty4.0,reids2.8,nginx等框架.目前是4台proxy服 ...

  4. HDFS的命令行操作

    1.namenode –format:格式化DFS 文件系统 2.secondaryNameNode: 运行DFS的 SecondaryNameNode 进程 hadoop secondaryname ...

  5. c#自带压缩类实现数据库表导出到CSV压缩文件的方法

    在导出大量CSV数据的时候,常常体积较大,采用C#自带的压缩类,可以方便的实现该功能,并且压缩比例很高,该方法在我的开源工具DataPie中已经经过实践检验.我的上一篇博客<功能齐全.效率一流的 ...

  6. 关于Firefox浏览器如何支持ActiveX控件,一个小的Hellow World

    今天尝试开发一个Firefox的插件.虽然比较简单,网上也有很多教程,但是感觉一些教程写的比较麻烦,在初步的开发过程中并没有用到那些东西,于是自己把开发过程记录下来.我是根据Mozilla官方教程开发 ...

  7. git rev-list

    git-rev-list - Lists commit objects in reverse chronological order 按照时间顺序倒序排列的commit Update: If all ...

  8. Function 1 - hello world

    Function 1 - hello world Make a simple function called greet that returns the most-famous "hell ...

  9. poj 2965 The Pilots Brothers' refrigerator枚举(bfs+位运算)

    //题目:http://poj.org/problem?id=2965//题意:电冰箱有16个把手,每个把手两种状态(开‘-’或关‘+’),只有在所有把手都打开时,门才开,输入数据是个4*4的矩阵,因 ...

  10. 宏HASH_DELETE

    HASH_DELETE(buf_page_t, hash, buf_pool->page_hash, fold, bpage); NAME 可理解为 void* next /********** ...