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. http://doc.okbase.net/congcong68/archive/112508.html

    http://doc.okbase.net/congcong68/archive/112508.html

  2. Android:控件ListView列表项与适配器结合使用

    Listview是用来展示一些重复性的数据用的,比如一些列表集合数据展示到手机,需要适配器作为载体获取数据,最后将数据填充到布局. ListView里面的每个子项Item可以使一个字符串,也可以是一个 ...

  3. Android:调试之LogCat

    通过 Logcat 查看: 常用的Log有5个:Log.v().Log.d().Log.i() .Log.w(). Log.e(). Log.i( "类":"函数名&qu ...

  4. C#基础精华07(委托事件,委托的使用,匿名方法)

    1.委托概述 委托是一种数据类型,像类一样(可以声明委托类型变量).方法参数可以是int.string.类类型 void M1(int n){  } √ void M2(string s){  } √ ...

  5. MYSQL 优化建议

    转自 http://coolshell.cn/articles/1846.html MYSQL 优化建议20条 1. 为查询缓存优化你的查询 大多数的MySQL服务器都开启了查询缓存.这是提高性最有效 ...

  6. POJ3660——Cow Contest(Floyd+传递闭包)

    Cow Contest DescriptionN (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a prog ...

  7. poj 1062 昂贵的聘礼(最短路 dijk+枚举)

    终于A 了,这题做着真麻烦 题目:http://poj.org/problem?id=1062 dijk 一般用于正权有向图 此题的关键在于等级限制的处理,最好的办法是采用枚举,即假设酋长等级为5,等 ...

  8. bzoj1937

    这道题没弄明白 初始模型很好想,是用到了最小生成树的性质 加入非树边后树上形成的环,非树边一定大于等于任意树边 然后考虑树边一定是缩小,非树边一定是增大 有di+wi>=dj-wj wi+wj& ...

  9. LeetCode Summary Ranges (统计有序数列范围)

    题意:给出个有序不重复数列(可能负数),用缩写法记录这个数列. 思路:找每个范围的起始和结束即可. class Solution { public: vector<string> summ ...

  10. 【Markdown】Writing on Github - 在GitHub上写作

    Writing on GitHub https://github.com/shalliestera/Writing-on-GitHub-Chinese-Translation Markdown 基本语 ...