Leetcode 289. Game of Life (Medium) (cpp)
来源:互联网 发布:macbook 装机必备软件 编辑:程序博客网 时间:2024/06/08 19:06
Leetcode 289. Game of Life (Medium) (cpp)
Tag: Array
Difficulty: Medium
/*289. Game of Life (Medium)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?*/class Solution {public:void gameOfLife(vector<vector<int>>& board) {for (int i = 0; i < board.size(); i++) {for (int j = 0; j < board[0].size(); j++) {int cnt = lives(board, i, j);if (cnt == 3 || (cnt == 2 && board[i][j] == 1)) { board[i][j] += 10; }}}update(board);}int lives(vector<vector<int>>& board, int i, int j) {int cnt = 0;for (int row = max(0, i - 1); row <= min((int)board.size() - 1, i + 1); row++) {for (int col = max(0, j - 1); col <= min((int)board[0].size() - 1, j + 1); col++) {if (row != i || col != j) { cnt += board[row][col] % 10; }}}return cnt;}void update(vector<vector<int>>& board) {for (int i = 0; i < board.size(); i++) {for (int j = 0; j < board[0].size(); j++) {board[i][j] = board[i][j] / 10;}}}};
0 0
- Leetcode 289. Game of Life (Medium) (cpp)
- Leetcode 289. Game of Life[medium]
- [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
- TCP握手/挥手的过程分析
- Junit4部分注解
- c#语言web页实现邮箱注册功能
- 分布式两阶段提交和三阶段提交
- 103.Which two statements are true regarding the Oracle Data Pump export and import operations? (Choo
- Leetcode 289. Game of Life (Medium) (cpp)
- 在浏览器和node中使用 GitHub的js-xlsx库进行Excel文件处理(一)
- LOGBACK 配置: 用 XML
- 序列化
- SVN 外部链接
- java基本概念
- org.quartz调度JOB执行时间超过调度周期的情况说明
- Vanya and Food Processor
- 基于物品的协同过滤算法