当前位置: 首页 > news >正文

LeetCode -- 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.


这里是康威生命游戏的规则:
https://zh.wikipedia.org/wiki/康威生命游戏
生命游戏中,对于任意细胞,规则如下:
每个细胞有两种状态-存活或死亡,每个细胞与以自身为中心的周围八格细胞产生互动。(如图,黑色为存活,白色为死亡)


当前细胞为存活状态时,当周围低于2个(不包含2个)存活细胞时, 该细胞变成死亡状态。(模拟生命数量稀少)
当前细胞为存活状态时,当周围有2个或3个存活细胞时, 该细胞保持原样。
当前细胞为存活状态时,当周围有3个以上的存活细胞时,该细胞变成死亡状态。(模拟生命数量过多)
当前细胞为死亡状态时,当周围有3个存活细胞时,该细胞变成存活状态。 (模拟繁殖)


思路:
对每个单元格依次判断,符合规则直接赋值就行了。


实现代码:


public class Solution {
    public void GameOfLife(int[,] board) 
    {
        var row = board.GetLength(0);
    	var col = board.GetLength(1);
    	
    	var board2 = new int[row, col];
    	for(var i = 0;i < row; i++){
    		for(var j = 0;j < col; j++){
    			board2[i,j] = board[i, j];
    		}
    	}
	
    	
    	for(var i = 0; i < row ; i++){
    		for(var j = 0;j < col ; j++){
    			var lives = 0;
    			Check(i, j, board2, out lives);
    			if(board2[i,j] == 1){
    				if(lives < 2){
    					board[i,j] = 0;
    				}
    				if(lives == 2 || lives == 3){
    					continue;
    				}
    				if(lives > 3){
    					board[i,j] = 0;
    				}
    			}
    			else{
    				if(lives == 3){
    					board[i,j] = 1;
    				}
    			}
    		}
    	}
    }


private void Check(int row, int col, int[,] board, out int lives)
{
	var rowLen = board.GetLength(0);
	var colLen = board.GetLength(1);
	
	lives = 0;
	
	// top neighbers
	if(row > 0){
		if(board[row - 1, col] == 1){
			lives ++;
		}
		if(col > 0 && board[row - 1, col - 1] == 1){
			lives ++;
		}
		if(col < colLen - 1 && board[row - 1 ,col + 1] == 1){
			lives ++;
		}
	}
	//left and right
	if(col > 0 && board[row, col - 1] == 1){
		lives ++;
	}
	if(col < colLen - 1 && board[row, col + 1] == 1){
		lives ++;
	}
	// below neighbers
	if(row < rowLen - 1)
	{
		if(col > 0 && board[row + 1, col - 1] == 1){
			lives ++;
		}
		if(board[row + 1, col] == 1){
			lives ++;
		}
		if(col < colLen - 1 && board[row + 1, col + 1] == 1){
			lives ++;
		}
	}
}


}


相关文章:

  • 黄舒骏,改变1995
  • LeetCode -- H-Index II
  • vim插件 ctags 和 taglist 的安装和使用
  • LeetCode -- Longest Increasing Subsequence
  • LeetCode -- Serialize and Deserialize Binary Tree
  • Ubuntu中用apt安装和卸载软件
  • LeetCode -- Single Number III
  • Linux 常用C函数(内存及字符串操作篇2)
  • LeetCode -- Subsets II
  • WCDMA与CDMA2000网络结构比较
  • LeetCode -- Integer to English Words
  • WiMAX组网技术与解决方案
  • LeetCode -- Sum Root to Leaf Numbers
  • 移动设备管理(MDM)与OMA(OTA)DM协议向导(三)——AAA服务器
  • LeetCode -- Surrounded Regions
  • 【347天】每日项目总结系列085(2018.01.18)
  • 【5+】跨webview多页面 触发事件(二)
  • Asm.js的简单介绍
  • C++11: atomic 头文件
  • Flex布局到底解决了什么问题
  • golang中接口赋值与方法集
  • Java Agent 学习笔记
  • Less 日常用法
  • PHP那些事儿
  • Protobuf3语言指南
  • 浏览器缓存机制分析
  • 面试总结JavaScript篇
  • 那些年我们用过的显示性能指标
  • 前端学习笔记之观察者模式
  • 前端之Sass/Scss实战笔记
  • 巧用 TypeScript (一)
  • 容器化应用: 在阿里云搭建多节点 Openshift 集群
  • 三分钟教你同步 Visual Studio Code 设置
  • 在Mac OS X上安装 Ruby运行环境
  • AI又要和人类“对打”,Deepmind宣布《星战Ⅱ》即将开始 ...
  • ​LeetCode解法汇总307. 区域和检索 - 数组可修改
  • #我与Java虚拟机的故事#连载11: JVM学习之路
  • (env: Windows,mp,1.06.2308310; lib: 3.2.4) uniapp微信小程序
  • (k8s中)docker netty OOM问题记录
  • (Redis使用系列) SpringBoot 中对应2.0.x版本的Redis配置 一
  • (附程序)AD采集中的10种经典软件滤波程序优缺点分析
  • (附源码)spring boot基于Java的电影院售票与管理系统毕业设计 011449
  • (附源码)小程序儿童艺术培训机构教育管理小程序 毕业设计 201740
  • (简单有案例)前端实现主题切换、动态换肤的两种简单方式
  • (论文阅读40-45)图像描述1
  • (免费分享)基于springboot,vue疗养中心管理系统
  • (十六)串口UART
  • (原)记一次CentOS7 磁盘空间大小异常的解决过程
  • (转)Android学习系列(31)--App自动化之使用Ant编译项目多渠道打包
  • (转)LINQ之路
  • .bat文件调用java类的main方法
  • .cn根服务器被攻击之后
  • .net core 微服务_.NET Core 3.0中用 Code-First 方式创建 gRPC 服务与客户端
  • .NET LINQ 通常分 Syntax Query 和Syntax Method
  • /usr/bin/perl:bad interpreter:No such file or directory 的解决办法