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 ...
随机推荐
- uva 437 hdu 1069
dp 将石块按三个面存入队列 按底面积排序 dp就最大高度 按嵌套矩形最长路做做法 #include <iostream> #include <cstdio> #inc ...
- 盘点 OSX 上最佳的 DevOps 工具
[编者按]对于运维人员来说,他们往往需要各种各样的工具来应对工作需求,近日 Dustin Collins 通过「The Best DevOps Tools on OSX」一文对 OSX 平台上的工具进 ...
- 在线学习SQL语句?没问题~~
以前弄得少,没注意.. http://sqlfiddle.com/ CREATE TABLE Presidents ( Id INT UNSIGNED NOT NULL AUTO_INCREMENT, ...
- ???????????? no permissions
1.一手鞋地址 google http://developer.android.com/tools/device.html 我处理的方法如下: 我的问题: android的版卡 在Ubuntu12.0 ...
- Maven打包时囊括本地依赖的jar包
在开发中,偶尔会遇到一个问题:某些比较冷门的包,maven服务器上没有,而我们又必须用,通常情况下会在项目中建立一个lib文件夹.将这些包copy进去并加入buildpath,开发就可以继续了,如下图 ...
- 编程添加"作为服务登录”权利(包括例子和API)
搜索"log on as a service programmatically" https://msdn.microsoft.com/en-us/library/windows/ ...
- nchar 和 nvarchar
字符数据类型(nchar 长度固定,nvarchar 长度可变)和 Unicode 数据使用 UNICODE UCS-2 字符集. nchar [ ( n ) ] n 个字符的固定长度的 Unicod ...
- Spring中的Resource
Spring中的资源定义:Resource此接口的全名为:org.springframework.core.io.Resource比较常用的资源定义的实现类为:1.ClassPathResource ...
- Java SE7新特性之try-with-resources语句
try-with-resources语句是一个声明一个或多个资源的 try 语句.一个资源作为一个对象,必须在程序结束之后随之关闭. try-with-resources语句确保在语句的最后每个 ...
- 重载操作符 operator overloading 学习笔记
重载操作符,只是另外一种调用函数的方法和表现方式,在某些情况它可以让代码更简单易读.注意不要过度使用重载操作符,除非它让你的类更简单,让你的代码更易读. 1语法 如下: 其中友元,关键字不是必须的,但 ...