【数组】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?
思路:
这里我们假设对于某个点,值的含义为
0 : 上一轮是0,这一轮过后还是0
1 : 上一轮是1,这一轮过后还是1
2 : 上一轮是1,这一轮过后变为0
3 : 上一轮是0,这一轮过后变为1
这样,对于一个节点来说,如果它周边的点是1或者2,就说明那个点上一轮是活的。最后,在遍历一遍数组,把我们编码再解回去,即0和2都变回0,1和3都变回1,就行了。
/**
* @param {number[][]} board
* @return {void} Do not return anything, modify board in-place instead.
*/
var gameOfLife = function(board) {
var m=board.length,n=board[0].length; for(var i=0;i<m;i++){
for(var j=0;j<n;j++){
var lives=0;
if(i>0){
lives+=board[i-1][j]==1||board[i-1][j]==2?1:0;
}
if(i<m-1){
lives+=board[i+1][j]==1||board[i+1][j]==2?1:0;
}
if(j>0){
lives+=board[i][j-1]==1||board[i][j-1]==2?1:0;
}
if(j<n-1){
lives+=board[i][j+1]==1||board[i][j+1]==2?1:0;
}
if(i>0&&j>0){
lives+=board[i-1][j-1]==1||board[i-1][j-1]==2?1:0;
}
if(i>0&&j<n-1){
lives+=board[i-1][j+1]==1||board[i-1][j+1]==2?1:0;
}
if(i<m-1&&j>0){
lives+=board[i+1][j-1]==1||board[i+1][j-1]==2?1:0;
}
if(i<m-1&&j<n-1){
lives+=board[i+1][j+1]==1||board[i+1][j+1]==2?1:0;
}
if(board[i][j] == 0 && lives == 3){
board[i][j] = 3;
} else if(board[i][j] == 1){
if(lives < 2 || lives > 3)
board[i][j] = 2;
}
}
} for(var i = 0; i < m; i++){
for(var j = 0; j < n; j++){
board[i][j] = board[i][j] % 2;
}
}
};
【数组】Game of Life的更多相关文章
- javascript中的Array对象 —— 数组的合并、转换、迭代、排序、堆栈
Array 是javascript中经常用到的数据类型.javascript 的数组其他语言中数组的最大的区别是其每个数组项都可以保存任何类型的数据.本文主要讨论javascript中数组的声明.转换 ...
- 探究javascript对象和数组的异同,及函数变量缓存技巧
javascript中最经典也最受非议的一句话就是:javascript中一切皆是对象.这篇重点要提到的,就是任何jser都不陌生的Object和Array. 有段时间曾经很诧异,到底两种数据类型用来 ...
- 编写高质量代码:改善Java程序的151个建议(第5章:数组和集合___建议75~78)
建议75:集合中的元素必须做到compareTo和equals同步 实现了Comparable接口的元素就可以排序,compareTo方法是Comparable接口要求必须实现的,它与equals方法 ...
- 了解PHP中的Array数组和foreach
1. 了解数组 PHP 中的数组实际上是一个有序映射.映射是一种把 values 关联到 keys 的类型.详细的解释可参见:PHP.net中的Array数组 . 2.例子:一般的数组 这里,我 ...
- JavaScript权威指南 - 数组
JavaScript数组是一种特殊类型的对象. JavaScript数组元素可以为任意类型,最大容纳232-1个元素. JavaScript数组是动态的,有新元素添加时,自动更新length属性. J ...
- JavaScript常见的五种数组去重的方式
▓▓▓▓▓▓ 大致介绍 JavaScript的数组去重问题在许多面试中都会遇到,现在做个总结 先来建立一个数组 var arr = [1,2,3,3,2,'我','我',34,'我的',NaN,NaN ...
- js:给定两个数组,如何判断他们的相对应下标的元素类型是一样的
题目: 给Array对象原型上添加一个sameStructureAs方法,该方法接收一个任意类型的参数,要求返回当前数组与传入参数数组(假定是)相对应下标的元素类型是否一致. 假设已经写好了Array ...
- javascript数组查重方法总结
文章参考地址:http://blog.csdn.net/chengxuyuan20100425/article/details/8497277 题目 对下列数组去重: var arr = ['aa', ...
- 掌握javascript中的最基础数据结构-----数组
这是一篇<数据结构与算法javascript描述>的读书笔记.主要梳理了关于数组的知识.部分内容及源码来自原作. 书中第一章介绍了如何配置javascript运行环境:javascript ...
- 小兔JS教程(四)-- 彻底攻略JS数组
在开始本章之前,先给出上一节的答案,参考答案地址: http://www.xiaotublog.com/demo.html?path=homework/03/index2 1.JS数组的三大特性 在J ...
随机推荐
- not allowed to access to crontab because of pam configuration
如果运行crontab如遇下面这样的错误: $ crontab -l You (zhangsan) are not allowed to access to (crontab) because of ...
- Codeforces777A Shell Game 2017-05-04 17:11 59人阅读 评论(0) 收藏
A. Shell Game time limit per test 0.5 seconds memory limit per test 256 megabytes input standard inp ...
- Linux 用 sftp scp命令 互传文件
sftp它类似于 ftp, 但它进行加密传输,比FTP有更高的安全性. sftp 是SSH服务的子程序 常用命令 pwd 查看当前工作目录 ls 查看远程当前目录下的所以文件或者目录信息 lls 查看 ...
- POJ1269求两个直线的关系平行,重合,相交
依旧是叉积的应用 判定重合:也就是判断给定的点是否共线的问题——叉积为0 if(!cross(p1,p2,p3) && !cross(p1,p2,p4))printf("LI ...
- [leetcode] 15. Plus One
这道题其实让我意识到了我的英文水平还有待加强.... 题目如下: Given a non-negative number represented as an array of digits, plus ...
- Android SQLiteOpenHelper Sqlite数据库的创建与打开
Android Sqlite数据库是一个怎样的数据库? 答:是一种嵌入式小型设备,移动设备,的数据库,应用在穿戴设备(例如:智能手表,计算手环 等等),移动设备(例如:Android系统类型的手机 等 ...
- DevExpress中Tile Application窗体的模型架构图
DEV中Tile Application模型架构比较复杂,整理一下和大家分享. 图中:立体代表类:虚线椭圆代表属性.
- Pipelines - .NET中的新IO API指引(三) 边看边记
Pipelines - .NET中的新IO API指引 作者 marcgravell 原文 此系列前两篇网上已有的译文 Pipelines - .NET中的新IO API指引(一) Pipeline ...
- js获取select下拉框的value值和text文本值
介绍一种取下拉框值以及绑定下拉框数据的方法 这里用到的jquery-ui-multiselect插件 1.前台html代码 <span class="ModuleFormFiel ...
- RabbitMQ基础入门篇
下载安装 Erlang RabbitMQ 启动RabbitMQ管理平台插件 DOS下进入到安装目录\sbin,执行以下命令 rabbitmq-plugins enable rabbitmq_manag ...