289. Game of Live
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?
本题题目比较长,其实就是如下的表格:
| neighbor | current state | next state |
| 1 | 1 | 0 |
| 2,3 | 1 | 1 |
| >4 | 1 | 0 |
| 3 | 0 | 1 |
这种题目和single number,single number2 比较相似,用的都是用位来存储状态。本题用第一位表示next state,第二位表示current state
代码如下:
//1->0 neighbor 1
//1->1 neighbor 2,3
//1->0 neightbor >4
//0->1 neightbor 3
public class Solution {
public void gameOfLife(int[][] board) {
if(board.length==) return;
for(int i=;i<board.length;i++){
for(int j=;j<board[].length;j++){
int live = liveneighbor(board,i,j);
if(board[i][j]==&&live>=&&live<=){
board[i][j]=(board[i][j]<<)+;
}else if(board[i][j]==&&live==){
board[i][j] = <<;
}
}
}
for(int i=;i<board.length;i++){
for(int j=;j<board[].length;j++){
board[i][j] = board[i][j]>>;
}
}
}
public int liveneighbor(int[][] board,int row,int col){
int m = board.length;
int n =board[].length;
int live = ;
for(int x = Math.max(,row-);x<=Math.min(m-,row+);x++){
for(int y=Math.max(,col-);y<=Math.min(n-,col+);y++){
live+=board[x][y]&;
}
}
live-=board[row][col];
return live;
}
}
289. Game of Live的更多相关文章
- leetcode@ [289] Game of Life (Array)
https://leetcode.com/problems/game-of-life/ According to the Wikipedia's article: "The Game of ...
- SCUT - 289 - 小O的数字 - 数位dp
https://scut.online/p/289 一个水到飞起的模板数位dp. #include<bits/stdc++.h> using namespace std; typedef ...
- 2017-3-9 leetcode 283 287 289
今天操作系统课,没能安心睡懒觉23333,妹抖龙更新,可惜感觉水分不少....怀念追RE0的感觉 =================================================== ...
- [LeetCode] 289. Game of Life 生命游戏
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...
- Java实现 LeetCode 289 生命游戏
289. 生命游戏 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有 ...
- 289. Game of Life -- In-place计算游戏的下一个状态
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...
- nyoj 289 苹果 动态规划 (java)
分析:0-1背包问题 第一次写了一大串, 时间:576 内存:4152 看了牛的代码后,恍然大悟:看来我现在还正处于鸟的阶段! 第一次代码: #include<stdio.h> #inc ...
- 贪心 Codeforces Round #289 (Div. 2, ACM ICPC Rules) B. Painting Pebbles
题目传送门 /* 题意:有 n 个piles,第 i 个 piles有 ai 个pebbles,用 k 种颜色去填充所有存在的pebbles, 使得任意两个piles,用颜色c填充的pebbles数量 ...
- 递推水题 Codeforces Round #289 (Div. 2, ACM ICPC Rules) A. Maximum in Table
题目传送门 /* 模拟递推水题 */ #include <cstdio> #include <iostream> #include <cmath> #include ...
- NYOJ-289 苹果 289 AC(01背包) 分类: NYOJ 2014-01-01 21:30 178人阅读 评论(0) 收藏
#include<stdio.h> #include<string.h> #define max(x,y) x>y?x:y struct apple { int c; i ...
随机推荐
- js 常用处理
判断浏览器环境是PC端还是手机端 function goPAGE() { if ((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios| ...
- Kickstart Round D 2017 : A
思路: 动态规划. large数据的时间范围很大,无法设计入状态中.转换思路为定义dp[i][j]为当前在景点i,并且已经游览了j个景点所花费的最小时间,这种思想与leetcode45类似.于是转移方 ...
- SDK manager.exe 运行时报错:系统找不到指定的文件 android.bat
android studio 2.3.1的 SDK Manager工具 突然没有 Launcher XXX 那个按钮,只好到SDK目录中去启动,无奈发生以下错误. 解决办法:运行android.bat ...
- 使用Jenkins进行android项目的自动构建(5)
之前在项目中引入的单元测试使用的是JUnit,可以在构建前进行测试,这里在介绍一下使用Instrumentation 进行单元测试.使用Instrumentation进行测试,比之前多一些步骤,需要把 ...
- jQuery的属性与样式之样式操作.css()
.css() 方法:获取元素样式属性的计算值或者设置元素的CSS属性 获取: .css( propertyName ) :获取匹配元素集合中的第一个元素的样式属性的计算值 .css( property ...
- xamarin 学习笔记02- IOS Simulator for windows 安装
微软发布了在window下的ios模拟器 下载 ios模拟器 并安装在windows系统上. Xamarin for Visual Studio 和 网络上的 Mac 中的 Xamarin.iOS 开 ...
- 如何优化LIMIT
首先我们先创建个数据表做测试 表名 test (id(int) , name(var char) , content(text) , pid(int) ) 往里面倒几百万条数据进去做测试. 我们都知道 ...
- qemu启动vm后,如何host上使用ssh连接?
qemu启动vm后,如何从host ssh连接vm? qemu启动参数 启动命令: ${cmd_qemu_system} --enable-kvm \ -machine type=q35,accel= ...
- shell脚本批量/单独启动、停止、重启java独立jar程序
本人最近半年使用阿里dubbo做开发,并在公司内部大力进行推广,将原来一个笨重且不易于维护的大项目切分成多个相对独立的java程序,好处是显而易见的,但是随着切分的独立运行程序包越来越多,程序的部署变 ...
- sql 触发器 针对一张表数据写入 另一张表 的增删改
ALTER TRIGGER [dbo].[tri_test2] ON [dbo].[student] for INSERT,DELETE,UPDATEAS BEGIN if not exists (s ...