leetcode笔记:Game of Life
2016-02-18 00:06
316 查看
一. 题目描述
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.
二. 题目分析
题目很长,大意如下:
给定一个由
当前细胞为存活状态时,当周围存活细胞不到2个时, 该细胞变成死亡状态。(模拟生命数量稀少)
当前细胞为存活状态时,当周围有2个或3个存活的细胞时, 该细胞保持原样。
当前细胞为存活状态时,当周围有3个以上的存活细胞时,该细胞变成死亡状态。(模拟生命数量过多)
当前细胞为死亡状态时,当周围恰好有3个存活细胞时,该细胞变成存活状态。 (模拟繁殖)
要求写一个函数,根据矩阵当前的状态,计算这个细胞矩阵的下一个状态。
题目要求in-place操作,考虑到每个元素都受附近8个元素的影响,必须使用一种中间状态来记录元素变化,这种中间状态应该能反应元素变化前和变化后的状态。
好在题目给出了四种变化状态,比较直观地给出了思路:
状态0: 死细胞->死细胞
状态1: 活细胞->活细胞
状态2: 活细胞->死细胞
状态3: 死细胞->活细胞
对每个元素进行判断,根据附近8个元素及本身的状态可得到状态
而对所有元素标记状态
三. 示例代码
四. 小结
该题目在in-place的要求下,是一道有趣的题目。
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.
二. 题目分析
题目很长,大意如下:
给定一个由
0,1组成的矩阵,每一个元素表示一个细胞的存活,1存活,0死亡,其中下一次更新每个细胞的存活由上、下、左、右、左上、左下、右上、右下,八个细胞决定,存活规则如下:
当前细胞为存活状态时,当周围存活细胞不到2个时, 该细胞变成死亡状态。(模拟生命数量稀少)
当前细胞为存活状态时,当周围有2个或3个存活的细胞时, 该细胞保持原样。
当前细胞为存活状态时,当周围有3个以上的存活细胞时,该细胞变成死亡状态。(模拟生命数量过多)
当前细胞为死亡状态时,当周围恰好有3个存活细胞时,该细胞变成存活状态。 (模拟繁殖)
要求写一个函数,根据矩阵当前的状态,计算这个细胞矩阵的下一个状态。
题目要求in-place操作,考虑到每个元素都受附近8个元素的影响,必须使用一种中间状态来记录元素变化,这种中间状态应该能反应元素变化前和变化后的状态。
好在题目给出了四种变化状态,比较直观地给出了思路:
状态0: 死细胞->死细胞
状态1: 活细胞->活细胞
状态2: 活细胞->死细胞
状态3: 死细胞->活细胞
对每个元素进行判断,根据附近8个元素及本身的状态可得到状态
0~4中的一个,而下一个元素依然可根据上一元素的状态
0~4,判断上一元素变化之前是什么状态。
而对所有元素标记状态
0~4后,再一次遍历矩阵,所有元素的状态对2取余,那么状态0和2就变成死细胞,状态1和3就是活细胞,达成目的。
三. 示例代码
[code]class Solution { public: void gameOfLife(vector<vector<int>>& board) { int rows=board.size(); if(rows==0) return ; int colums=board[0].size(); if(colums==0) return ; for(int i=0; i<rows; ++i) { for(int j=0; j<colums; ++j) { int sum=getNeighbor(board,rows,colums,i,j); if(sum==2) continue; else if(sum==3) board[i][j]=board[i][j]==0?3:1; else board[i][j]=board[i][j]==1?2:0; } } for(int i=0;i<rows;++i) { for(int j=0;j<colums;++j) board[i][j]%=2; } } private: int getNeighbor(vector<vector<int>>& board,int rows,int colums,int x,int y) { int sum=0; for(int i=x-1; i<x+2; ++i) { for(int j=y-1; j<y+2; ++j) { if(i==x&&j==y) continue; if(i>=0&&i<rows&&j>=0&&j<colums&&(board[i][j]==1||board[i][j]==2)) ++sum; } } return sum; } };
四. 小结
该题目在in-place的要求下,是一道有趣的题目。
相关文章推荐
- iscroll.js的上拉下拉刷新时无法回弹的解决方法
- Angular发布1.5正式版,专注于向Angular 2的过渡
- JavaScript+canvas实现七色板效果实例
- AngularJS 2.0新特性有哪些
- jQuery插件实现适用于移动端的地址选择器
- XML、HTML、CSS与JS的区别整理
- js实现简单的省市县三级联动效果实例
- JavaScript与jQuery实现的闪烁输入效果
- Js实现简单的小球运动特效
- 基于jQuery实现仿搜狐辩论投票动画代码(附源码下载)
- JS实现1000以内被3或5整除的数字之和
- 浏览器CSS Hack
- 利用css样式画各种图形
- ( 译、持续更新 ) JavaScript 上分小技巧(四)
- JS 中给按钮动态添加触发事件
- Grunt配置之 -- JSHint插件安装和配置
- 如何修改Liferay的Session过期时间
- jQuery文本框表情插件
- node相关
- 解决iscroll.js上拉下拉刷新手指划出屏幕页面无法回弹问题