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 ...
随机推荐
- jq判断上下滚动
$(document).ready(function(){ var p=0,t=0; $(window).scroll(function(e){ p = $(this).scrollTop(); if ...
- Android开发使用的常见第三方框架汇总
本文转载:http://blog.csdn.net/liuhaomatou/article/details/44857005 1.volley 项目地址 https://github.com/sman ...
- js获取select选中的标签option的值
js中获取方法 var obj = document.getElementByIdx_xx_x(”testSelect”); //定位id var index = obj.selectedInde ...
- mvc框架 与vuex的介绍
应用级的状态集中放在store中: 改变状态的方式是提交mutations,这是个同步的事物: 异步逻辑应该封装在action中. const vuex_store = new Vuex.store( ...
- photoshop cs6安装和破解步骤
http://tieba.baidu.com/p/4791130877 http://www.frontopen.com/1181.html
- iOS重签
由于渠道推广需要,可能需要多个包做备份推广,区别是icon.游戏名称.登录logo.bundleid.签名证书.支付Consumables不同,其他游戏包体完全相同. 反复修改多次文件提交Jenkin ...
- cf536c——思路题
题目 题目:Lunar New Year and Number Division 题目大意:给定一个数字序列,可以任意分组(可调整顺序),但每组至少两个,求每组内数字和的平方的最小值 思路 首先,易证 ...
- hystrix 解决服务雪崩效应
1.服务雪崩效应 默认情况下tomcat只有一个线程池去处理客户端发送的所有服务请求,这样的话在高并发情况下,如果客户端所有的请求堆积到同一个服务接口上, 就会产生tomcat的所有线程去处理该服务接 ...
- cesium primitive方式 ————http://blog.sina.com.cn/s/blog_15e866bbe0102y0ji.html
Cesium学习笔记-工具篇17-PrimitivePoint自定义渲染-点 (2018-08-28 16:12:06) 转载▼ 标签: cesium primitive 自定义渲染 shader c ...
- windows sdk 设置窗体透明
#define WINVER 0x0501 #include <windows.h> /* Declare Windows procedure */ LRESULT CALLBACK Wi ...