LeetCode //C - 289. Game of Life

289. Game of Life

According to 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.”

The board is made up of an m x n grid of cells, where each cell has an initial state: live (represented by a 1) or dead (represented by a 0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):

  1. Any live cell with fewer than two live neighbors dies as if caused by under-population.
  2. Any live cell with two or three live neighbors lives on to the next generation.
  3. Any live cell with more than three live neighbors dies, as if by over-population.
  4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the m x n grid board, return the next state.
 

Example 1:

在这里插入图片描述

Input: board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
Output: [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]

Example 2:

在这里插入图片描述

Input: board = [[1,1],[1,0]]
Output: [[1,1],[1,1]]

Constraints:

  • m == board.length
  • n == board[i].length
  • 1 <= m, n <= 25
  • board[i][j] is 0 or 1.

Follow up:

  • Could you solve it in-place? Remember that the board needs to be updated simultaneously: 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 upon the border of the array (i.e., live cells reach the border). How would you address these problems?

From: LeetCode
Link: 289. Game of Life


Solution:

Ideas:

I first create a function countLiveNeighbors to count the live neighbors of a given cell.

In the gameOfLife function, I create a copy of the board to maintain the original state of the board while applying the rules.

Then, for each cell in the board, I calculate the number of live neighbors and apply the four rules given in the problem statement.

Finally, I deallocate the memory reserved for the copied board to avoid memory leaks.

Code:
int countLiveNeighbors(int** board, int boardSize, int* boardColSize, int row, int col) {int liveNeighbors = 0;int rowDirections[8] = {-1, -1, -1, 0, 0, 1, 1, 1};int colDirections[8] = {-1, 0, 1, -1, 1, -1, 0, 1};for (int i = 0; i < 8; i++) {int newRow = row + rowDirections[i];int newCol = col + colDirections[i];if (newRow >= 0 && newRow < boardSize && newCol >= 0 && newCol < boardColSize[row]) {liveNeighbors += board[newRow][newCol];}}return liveNeighbors;
}void gameOfLife(int** board, int boardSize, int* boardColSize) {int** copyBoard = (int**) malloc(boardSize * sizeof(int*));for (int i = 0; i < boardSize; i++) {copyBoard[i] = (int*) malloc(boardColSize[i] * sizeof(int));for (int j = 0; j < boardColSize[i]; j++) {copyBoard[i][j] = board[i][j];}}for (int row = 0; row < boardSize; row++) {for (int col = 0; col < boardColSize[row]; col++) {int liveNeighbors = countLiveNeighbors(copyBoard, boardSize, boardColSize, row, col);if (copyBoard[row][col] == 1 && (liveNeighbors < 2 || liveNeighbors > 3)) {board[row][col] = 0;}else if (copyBoard[row][col] == 0 && liveNeighbors == 3) {board[row][col] = 1;}}}for (int i = 0; i < boardSize; i++) {free(copyBoard[i]);}free(copyBoard);
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/53017.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

JVM基础篇-方法区与运行时常量池

JVM基础篇-方法区与运行时常量池 方法区 Java 虚拟机有一个在所有 Java 虚拟机线程之间共享的方法区。方法区类似于传统语言的编译代码的存储区或者类似于操作系统进程中的“文本”段。它存储每个类的结构&#xff0c;例如运行时常量池、字段和方法数据&#xff0c;以及方法和…

Hyper实现git bash在windows环境下多tab窗口显示

1.电脑上安装有git bash 下载链接&#xff1a;https://gitforwindows.org/ 安装Hyper 下载链接:官网 https://hyper.is/ 或者在百度云盘下载&#xff1a; https://pan.baidu.com/s/1BVjzlK0s4SgAbQgsiK1Eow 提取码&#xff1a;0r1f 设置 打开Hyper&#xff0c;依次点左上角-&g…

(学习笔记-进程管理)进程

进程 我们编写的代码只是一个存储在硬盘的静态文件&#xff0c;通过编译后会生成二进制可执行文件&#xff0c;当我们运行这个可执行文件后&#xff0c;它会被装载到内存中&#xff0c;接着CPU会执行程序中的每一条指令&#xff0c;那么这个运行中的程序就被称为进程。 现在我…

【C++】——内存管理

目录 回忆C语言内存管理C内存管理方式new deleteoperator new与operator delete函数new和delete的实现原理定位new表达式(placement-new)malloc/free和new/delete的区别 回忆C语言内存管理 void Test() {int* p1 (int*)malloc(sizeof(int));free(p1);int* p2 (int*)calloc(4…

攻防世界-reverse-logmein

题目描述&#xff1a;菜鸡开始接触一些基本的算法逆向了 下载附件&#xff0c;是一个可执行程序 1. 思路分析 逆向出来看看代码 从代码中来看&#xff0c;密码长度需要和V8相等&#xff0c;并且每一个字符的运算结果需要满足 s[i] (char)(v8[i % v6 - 8] ^ v8[i]) 但是这…

openGauss学习笔记-31 openGauss 高级数据管理-索引

文章目录 openGauss学习笔记-31 openGauss 高级数据管理-索引31.1 语法格式31.2 参数说明31.3 示例 openGauss学习笔记-31 openGauss 高级数据管理-索引 索引是一个指向表中数据的指针。一个数据库中的索引与一本书的索引目录是非常相似的。 索引可以用来提高数据库查询性能&…

从零开始理解Linux中断架构(24)软中断核心函数__do_softirq

1)概要 __do_softirq函数处理是总是尽可能的执行所有未决软中断。 (1)关闭软中断:在preempt_count设置软中断标志:SOFTIRQ_OFFSET 让in_interrupt检查条件为真,进入软中断处理临界区,后面进来的处理请求,需要检查in_interrupt(),从而达到禁止本cpu上的软中断嵌套的目…

【雕爷学编程】MicroPython动手做(33)——物联网之天气预报

天气&#xff08;自然现象&#xff09; 是指某一个地区距离地表较近的大气层在短时间内的具体状态。而天气现象则是指发生在大气中的各种自然现象&#xff0c;即某瞬时内大气中各种气象要素&#xff08;如气温、气压、湿度、风、云、雾、雨、闪、雪、霜、雷、雹、霾等&#xff…

Java三大特征之继承【超详细】

文章目录 一、继承概念二、继承的语法三、父类成员访问3.1子类中访问父类的成员变量3.2子类和父类成员变量同名3.3子类中访问父类的成员方法 四、super关键字五、子类构造方法六、super和this七、再谈初始化八、protected 关键字九、继承方式十、final 关键字十一、继承与组合 …

springboot+vue农业技术信息管理系统_9927h

随着信息时代的发展&#xff0c;计算机迅速普及&#xff0c;传统的农业信息管理方式显得不够快捷&#xff0c;这时我们就需要创造更加便利的管理方法&#xff0c;对农业信息进行统计&#xff0c;便于统一管理。将传统管理方式转变为信息、智能化显得尤为重要&#xff0c;农业信…

选读SQL经典实例笔记18_Exactly

1. 问题9 1.1. 只讲授一门课程的教授 1.2. sql select p.*from professor p,teach twhere p.lname t.lnameand p.lname not in ( select t1.lnamefrom teach t1,teach t2where t1.lname t2.lnameand t1.cno &#xff1e; t2.cno ) LNAME DEPT SALARY …

MIT 6.824 -- MapReduce -- 01

MIT 6.824 -- MapReduce -- 01 引言抽象和实现可扩展性可用性(容错性)一致性MapReduceMap函数和Reduce函数疑问 课程b站视频地址: MIT 6.824 Distributed Systems Spring 2020 分布式系统 推荐伴读读物: 极客时间 – 大数据经典论文解读DDIA – 数据密集型应用大数据相关论文…