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模块
原地址:http://www.9miao.com/question-15-54380.html Firefly是免费开源的游戏服务器端框架,开发语言是python,基于twisted框架开发,作为一名 ...
- linux 配置java环境变量
修改/etc/profile文件 如果你的计算机仅仅作为开发使用时推荐使用这种方法,因为所有用户的shell都有权使用这些环境变量,可能会给系统带来安全性问题. ·用文本编辑器打开/etc/profi ...
- ANDROID_MARS学习笔记_S01_012_RatingBar
1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...
- Android:布局实例之常见用户设置界面
实现效果: 整理思路: 1.控件:文字TextView 和 右箭头ImageView 2.因为考虑到点击效果,设计为:最外层为全圆角,内层有四种情况,分别为上圆角.无圆角.下圆角和全圆角. 3.内层样 ...
- Spring与Struts2整合
Spring与Struts2为什么要整合呢? 把Action实例交给Spring来管理!! 1.单独测试Struts是否添加成功(jar包和配置文件),先单独测试,不要整合之后再测试,容易出问题 we ...
- 最短路径算法之一——Floyd算法
Floyd算法 Floyd算法可以用来解决任意两个顶点之间的最短路径问题. 核心公式为: Edge[i][j]=Min{Edge[i][j],Edge[i][k]+Edge[k][j]}. 即通过对i ...
- Windows平台下的session0创建进程的问题与解决办法
很多博客都有记载如何在session0下创建进程的办法,也就是使用CreateProcessAsUser.但是这个要求服务的进程有SE_INCREASE_QUOTA_NAME和SE_ASSIGNPRI ...
- POJ3608(旋转卡壳--求两凸包的最近点对距离)
题目:Bridge Across Islands 分析:以下内容来自:http://blog.csdn.net/acmaker/article/details/3178696 考虑如下的算法, 算法的 ...
- CentOS7.1 安装关键步骤 记录下来
SecureCRT下载地址 https://yunpan.cn/cS9W94kuvhXPb 访问密码 08cd[这里GNOME桌面 下的 要全选,截屏有误]
- 高难度(1)什么是AR
在介绍增强现实(AR)之前,需要先说说虚拟现实(VR) 虚拟现实是从英文Virtual Reality 一词翻译过来的,简称VR.VR 技术是采用以计算机技术为核心的技术,生成逼真的视.听.触觉等一体 ...