LeetCode 289. Game of Life
来源:互联网 发布:淘宝分类模块代码 编辑:程序博客网 时间:2024/05/22 14:17
#include <vector>#include <iostream>using namespace std;/* Given a board with m by n cells, each cell has an initial state live(1) and dead(0). Each cell interacts with eight neighbors. using the flollowing four rules. 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 neightbors 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 neightbors 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.*/int allCounts(vector< vector<int> >& matrix, int i, int j) { int count = 0; if(i - 1 >= 0) count += matrix[i-1][j]; if(i + 1 < matrix.size()) count += matrix[i+1][j]; if(j - 1 >= 0) count += matrix[i][j-1]; if(j + 1 < matrix[0].size()) count += matrix[i][j+1]; if((i - 1 >= 0) && (j - 1 < matrix[0].size())) count += matrix[i-1][j-1]; if((i - 1 >= 0) && (j + 1 < matrix[0].size())) count += matrix[i-1][j+1]; if((i + 1 < matrix.size()) && (j - 1 < matrix[0].size())) count += matrix[i+1][j-1]; if((i + 1 < matrix.size()) && (j + 1 < matrix[0].size())) count += matrix[i+1][j+1]; return count;}void gameOfLife(vector< vector<int> >& matrix) { if(matrix.size() == 0 || matrix[0].size() == 0) return; vector< vector<int> > nextState(matrix.size(), vector<int>(matrix[0].size(), 0)); for(int i = 0; i < matrix.size(); ++i) { for(int j = 0; j < matrix[i].size(); ++j) { int tmp = allCounts(matrix, i, j); if((matrix[i][j] == 0) && (tmp == 3)) nextState[i][j] = 1; if(matrix[i][j] == 1) { if(tmp < 2) nextState[i][j] = 0; else if(tmp > 3) nextState[i][j] = 0; else nextState[i][j] = 1; } } } matrix = nextState;}
int main(void) { vector< vector<int> > matrix { {0, 1, 0, 1}, {1, 0, 0, 0}, {0, 0, 0, 0}, {1, 1, 1, 1}}; gameOfLife(matrix); for(int i = 0; i < matrix.size(); ++i) { for(int j = 0; j < matrix[i].size(); ++j) { cout << matrix[i][j] << " "; } cout << endl; }}
0 0
- [leetcode] 289. Game of Life
- 289. Game of Life LeetCode
- leetcode 289. Game of Life
- [LeetCode]289. Game of Life
- LeetCode 289. Game of Life
- LeetCode *** 289. Game of Life
- 【leetcode】289. Game of Life
- LeetCode 289. Game of Life
- LeetCode-289.Game of Life
- [Leetcode]289. Game of Life
- LeetCode--289. Game of Life
- [leetcode] 289. Game of Life
- [LeetCode] 289. Game of Life
- [leetcode]289. Game of Life
- [LeetCode]289. Game of Life
- [LeetCode]289. Game of Life
- 【LeetCode】 289. Game of Life
- LeetCode 289. Game of Life
- 【Baltic2014】【BZOJ3917】Sequence
- swift post json and header
- 集合与数组之间的转化
- NYOJ 207 POJ 1182 带权并查集做法(√)
- 【BZOJ1433】【codevs2347】假期的宿舍,最大流
- LeetCode 289. Game of Life
- linux基础(8)--用户和组的管理--RHEL6.5
- synchronized 实现原理分析
- ReentrantLock实现原理分析
- java的Arrays类的应用
- Android官方文档之App Components(Fragments)
- LeetCode 204. Count Primes(统计素数)
- Parameter和Attribute的区别
- 潜在的威胁——构造器内的多态方法行为