本文主要是介绍LeetCode 题解(261) : Game of Life,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
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?
面Dropbox的时候被问到了这道题。
C++版:
class Solution {
public:void gameOfLife(vector<vector<int>>& board) {if(board.size() == 0)return;vector<pair<int, int>> change;for(int i = 0; i < board.size(); i++) {for(int j = 0; j < board[0].size(); j++) {int n = check(board, i-1, j-1) + check(board, i-1, j) + check(board, i-1, j+1) +check(board, i, j-1) + check(board, i, j+1) +check(board, i+1, j-1) + check(board, i+1, j) + check(board, i+1, j+1);if(board[i][j] == 0 && n == 3) {change.push_back(pair<int, int>(i, j));}if(board[i][j] == 1 && (n < 2 || n > 3)) {change.push_back(pair<int, int>(i, j));}}}for(auto i : change) {if(board[i.first][i.second] == 1)board[i.first][i.second] = 0;elseboard[i.first][i.second] = 1;}}int check(vector<vector<int>>& board, int i, int j) {if(i < 0 || j < 0 || i >= board.size() || j >= board[0].size())return 0;if(board[i][j] == 1)return 1;elsereturn 0;}
};
Python版:
class Solution(object):def gameOfLife(self, board):""":type board: List[List[int]]:rtype: void Do not return anything, modify board in-place instead."""if len(board) == 0:returnchange = []for i in range(len(board)):for j in range(len(board[0])):n = self.check(board, i - 1, j - 1) + self.check(board, i - 1, j) + self.check(board, i - 1, j + 1) + \self.check(board, i, j - 1) + self.check(board, i, j + 1) + \self.check(board, i + 1, j - 1) + self.check(board, i + 1, j) + self.check(board, i + 1, j + 1)if board[i][j] == 0 and n == 3:change.append(tuple([i, j]))if board[i][j] == 1 and (n < 2 or n > 3):change.append(tuple([i, j]))for i in change:if board[i[0]][i[1]] == 1:board[i[0]][i[1]] = 0else:board[i[0]][i[1]] = 1def check(self, board, i, j):if i < 0 or j < 0 or i >= len(board) or j >= len(board[0]):return 0if board[i][j] == 1:return 1else:return 0
这篇关于LeetCode 题解(261) : Game of Life的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!