当前位置: 首页 > 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
  • canvas 绘制双线技巧
  • codis proxy处理流程
  • Git同步原始仓库到Fork仓库中
  • mockjs让前端开发独立于后端
  • node-glob通配符
  • Spring技术内幕笔记(2):Spring MVC 与 Web
  • webpack+react项目初体验——记录我的webpack环境配置
  • 第十八天-企业应用架构模式-基本模式
  • 动态魔术使用DBMS_SQL
  • 关于使用markdown的方法(引自CSDN教程)
  • 简单数学运算程序(不定期更新)
  • 罗辑思维在全链路压测方面的实践和工作笔记
  • 学习Vue.js的五个小例子
  • 用Python写一份独特的元宵节祝福
  • 原创:新手布局福音!微信小程序使用flex的一些基础样式属性(一)
  • 机器人开始自主学习,是人类福祉,还是定时炸弹? ...
  • 昨天1024程序员节,我故意写了个死循环~
  • ​linux启动进程的方式
  • #《AI中文版》V3 第 1 章 概述
  • #Java第九次作业--输入输出流和文件操作
  • #NOIP 2014# day.2 T2 寻找道路
  • (一)搭建springboot+vue前后端分离项目--前端vue搭建
  • *_zh_CN.properties 国际化资源文件 struts 防乱码等
  • ./configure、make、make install 命令
  • .equals()到底是什么意思?
  • .NET Core SkiaSharp 替代 System.Drawing.Common 的一些用法
  • .net core 连接数据库,通过数据库生成Modell
  • .net 发送邮件
  • .net 中viewstate的原理和使用
  • .NET/C# 中你可以在代码中写多个 Main 函数,然后按需要随时切换
  • .net2005怎么读string形的xml,不是xml文件。
  • .netcore 6.0/7.0项目迁移至.netcore 8.0 注意事项
  • .netcore如何运行环境安装到Linux服务器
  • /etc/fstab 只读无法修改的解决办法
  • /var/lib/dpkg/lock 锁定问题
  • []利用定点式具实现:文件读取,完成不同进制之间的
  • []我的函数库
  • [AHOI2009]中国象棋 DP,递推,组合数
  • [AutoSar NVM] 存储架构
  • [c]统计数字