Game of Life
来源:互联网 发布:c语言指针是什么 编辑:程序博客网 时间:2024/05/12 02:56
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.
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?
class Solution {public: void gameOfLife(vector<vector<int>>& board) { int r = board.size(); int c = board[0].size(); for(int i = 0; i < r; ++i) { for(int j = 0; j < c; ++j) { int tmp = 0; if(i > 0 && (board[i - 1][j] == 1 || board[i - 1][j] == 2)) tmp++; if(i < r - 1 && (board[i + 1][j] == 1 || board[i + 1][j] == 2)) tmp++; if(j > 0 && (board[i][j - 1] == 1 || board[i][j - 1] == 2)) tmp++; if(j < c - 1 && (board[i][j + 1] == 1 || board[i][j + 1] == 2)) tmp++; if(i > 0 && j > 0 && (board[i - 1][j - 1] == 1 || board[i - 1][j - 1] == 2)) tmp++; if(i > 0 && j < c - 1 && (board[i - 1][j + 1] == 1 || board[i - 1][j + 1] == 2)) tmp++; if(i < r - 1 && j > 0 && (board[i + 1][j - 1] == 1 || board[i + 1][j - 1] == 2)) tmp++; if(i < r - 1 && j < c - 1 && (board[i + 1][j + 1] == 1 || board[i + 1][j + 1] == 2)) tmp++; if(tmp < 2) { if(board[i][j] == 0) board[i][j] = 0;// die - die = 0 if(board[i][j] == 1) board[i][j] = 2;// live - die = 2 } if(tmp == 2 || tmp == 3) { if(board[i][j] == 1) board[i][j] = 1;// live - live = 1 if(board[i][j] == 0 && tmp == 3) board[i][j] = 3;// die - live = 3 if(board[i][j] == 0 && tmp == 2) board[i][j] = 0;// die - die } if(tmp > 3) { if(board[i][j] == 0) board[i][j] = 0;// die - die if(board[i][j] == 1) board[i][j] = 2;// live - die } } } for(int i = 0; i < r; ++i) { for(int j = 0; j < c; ++j) { if(board[i][j] == 2) board[i][j] = 0; else if(board[i][j] == 3) board[i][j] = 1; } } }};
0 0
- Game of Life
- Game Of Life
- LeetCode:Game of Life
- LeetCode: Game of Life
- Game of Life
- leetcode289 : Game of Life
- 【LeetCode】Game of Life
- Game of Life
- Game of Life
- LeetCode Game of Life
- Leetcode Game of Life
- Game of Life
- Game of Life
- [289]Game of Life
- [LeetCode] Game of Life
- Game Of Life
- LeetCode -- Game of Life
- 【leetcode】Game of Life
- 对冒泡排序的理解
- 你为什么创业失败?
- fs4412开发板学习笔记(十六)
- html标记语言的常用标签,表格
- IOS基础 002 我的第一个界面,login界面
- Game of Life
- SpringMVC-----2、SpringMVC入门
- fs4412开发板学习笔记(十七)
- 2014找工作----扎实的基础和开阔的视野是企业最看重的因素
- list分页
- Linux之旅--curl和Http协议
- AIDL 使用学习
- fs4412开发板学习笔记(十八)
- C++ 参数传递,定义与声明,命名空间等基本知识