Leetcode 289. Game of Life
时间:2022-05-06
本文章向大家介绍Leetcode 289. 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和1只占用最后一位,那么可以用其他bit位存储更新后的状态。
class Solution {
public:
void gameOfLife(vector<vector<int>>& board) {
int dir[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
int n = board.size();
if(!n) return;
int m = board[0].size();
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
int cnt = 0;
for(int k = 0; k < 8 ; k++)
{
int x = i + dir[k][0], y = j + dir[k][1];
if(x < 0 || x >= n || y < 0 || y >= m) continue;
if(board[x][y] & 1) cnt++;
}
if(cnt == 2) board[i][j] |= (board[i][j] & 1) << 1;
else if(cnt == 3) board[i][j] |= 1 << 1;
}
}
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
board[i][j] >>= 1;
}
};
- Android学习第一弹:Android通过用代码画虚线椭圆边框背景来学习一下shape的用法
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- iOS iOS与html进行交互
- Android学习第三弹之Android图片颜色处理
- C#Winform使用扩展方法自定义富文本框(RichTextBox)字体颜色
- Android如何实现毛玻璃效果之Android高级模糊技术
- Android Loader详解
- Gridview的item含有checkbox,setOnItemClickListener方法失效的问题
- 有效解决Android加载大图片内存溢出的问题
- 第七章 正则表达式编程
- android 实现倒影
- InvokeHelper类
- Android官方支持百分比设置宽和高的控件及示例
- Extjs radio布局--横向排列
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- LoRa节点开发——代码详解LoRaWAN发送与接收数据
- java编程思想第四版第十四章 类型信息习题
- 聊一聊,如何解密、分析LoRaWAN数据包?
- java编程思想第四版第十四章 类型信息总结
- 10 . Python之面向对象
- 11 . Python3之异常,调试和测试
- 张孝祥java高新技术 --- jkd1.5 新特性 -- 精华总结
- 12 . Python3之网络编程
- 01 . Zabbix简介原理及部署
- 03 . Zabbix自定义Key及配置触发器邮件报警
- 详解STM32+SX1276 Linux下的编译环境搭建
- 01 . Go语言简介及语言特征
- 02 . Go语言的变量,常量,作用域
- jquery多组时间以对象数组形式保存
- java下读取键盘输入的简单方法