leetcode@ [289] Game of Life (Array)
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):
- 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?
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)的更多相关文章
- 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 ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- LeetCode: Search in Rotated Sorted Array II 解题报告
Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...
- [LeetCode] 289. Game of Life 生命游戏
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...
- [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 ...
- [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 ...
- [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 ...
- [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
随机推荐
- firefly 环境配置所需工具
原地址:http://www.9miao.com/question-15-59032.html http://down.9miao.com/attachment/forum/201405/19/120 ...
- ie6 iframe src="javascript:" 报安全警报问题
<iframe id="shuaka_iframe" class="embed-page-iframe" data-src="https://w ...
- 【C++基础】关键字static 局部变量
1.局部变量 static局部变量和普通局部变量有什么区别:static局部变量只被初始化一次,下一次依据上一次结果值: int test(int j){ static int i=10; i=i+j ...
- Python的作用域
Python的作用域 转自:http://www.cnblogs.com/frydsh/archive/2012/08/12/2602100.html Python是静态作用域语言,尽管它自身是一个动 ...
- Interface Serializable
public interface Serializable Serializability of a class is enabled by the class implementing the ja ...
- WIN7+Ubuntu双系统,win7启动不了
在网上搜索了下,大多说的是因为重装引起的坏道, 我经过半天的搜索才找到了问题所在,首先看看下面连接的二楼大神给出的解决方案: https://forum.ubuntu.org.cn/viewtopic ...
- codeforces #309 div1 D
求最小值最大显然是要二分 二分之后转换成了判定性问题 我们考虑哪些点一定不能选 显然是将所有可选点选中之后依然不满足条件的点不能选 那么我们不妨维护一个堆,每次取出堆顶看看是否满足条件 不满足条件就p ...
- 吐槽C++
个人感觉,在c++ 道路的学习路上,遇到很多的坎坷,现在回想起来,最关键一点就是 c++知识点繁杂很多,教科书很多知识点都没有提到. 但是在实际工作中,这些没有提到的知识点,却又经常会用到(或者看开源 ...
- mpi冒泡排序并行化
一.实验目的与实验要求 1.实验目的 (1)学会将串行程序改为并行程序. (2)学会mpich2的使用. (3)学会openmp的配置. (4)mpi与openmp之间的比较. 2.实验要求 (1)将 ...
- 网上图书商城项目学习笔记-014购物车模块页面javascrip
一.流程分析 二.代码 1.view层 (1)list.jsp <%@ page language="java" import="java.util.*" ...