In this Leetcode Game of Life problem solution According to 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."
The board is made up of an m x n grid of cells, where each cell has an initial state: live (represented by a 1) or dead (represented by a 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.
The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the m x n grid board, return the next state.
Problem solution in Python.
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: kill=[] reproduce=[] directions=[(0,1),(1,0),(0,-1),(-1,0),(-1,-1),(1,-1),(-1,1),(1,1)] m,n=len(board),len(board[0]) for i in range(m): for j in range(n): if board[i][j]==0: live_nei=0 for d in directions: ni,nj=i+d[0],j+d[1] if 0<=ni<m and 0<=nj<n and board[ni][nj]==1: live_nei+=1 if live_nei==3: reproduce.append((i,j)) else: live_nei=0 for d in directions: ni,nj=i+d[0],j+d[1] if 0<=ni<m and 0<=nj<n and board[ni][nj]==1: live_nei+=1 if live_nei<2 or live_nei>3: kill.append((i,j)) for i,j in kill: board[i][j]=0 for i,j in reproduce: board[i][j]=1
Problem solution in Java.
class Solution { private static final int ZERO_TO_ONE = 2; private static final int ONE_TO_ZERO = 3; public void gameOfLife(int[][] board) { if (board.length == 0) return; for (int i = 0; i < board.length; i++) for (int j = 0; j < board[0].length; j++) { int live = getNeighbours(board, i, j); transform(board, i, j, live); } retransform(board); } public int getNeighbours(int[][] board, int i, int j) { int[] dir = { -1, 0, 1 }; int n = 0, d = 0; for (int di : dir) { int ni = i + di; if (ni >= 0 && ni < board.length) { for (int dj : dir) { int nj = j + dj; if (nj >= 0 && nj < board[0].length && !(ni == i && nj == j) && board[ni][nj] % 2 != 0) n++; } } } return n; } private void retransform(int[][] board) { for (int i = 0; i < board.length; i++) for (int j = 0; j < board[0].length; j++) { if (board[i][j] == 3) board[i][j] = 0; else if (board[i][j] == 2) board[i][j] = 1; } } public void transform(int[][] board, int i, int j, int live) { if (board[i][j] % 2 == 1) { if (live < 2 || live > 3) { board[i][j] = ONE_TO_ZERO; } } else if (live == 3) board[i][j] = ZERO_TO_ONE; } }
Problem solution in C++.
class Solution { public: void gameOfLife(vector<vector<int>>& board) { if(!board.size()) return; int i, j, m=board.size(), n=board[0].size(); unordered_map<int, int> mp={{0, 0}, {1, 1}, {2, 0}, {3, 1}}; for(i=0; i<m; i++){ for(j=0; j<n; j++){ int a=(i-1>=0 ? mp[board[i-1][j]] : 0)+(i-1>=0 && j+1<n ? mp[board[i-1][j+1]] : 0)+ (i-1>=0 && j-1>=0 ? mp[board[i-1][j-1]] : 0)+(j-1>=0 ? mp[board[i][j-1]] : 0)+ (j+1<n ? mp[board[i][j+1]] : 0)+(i+1<m ? mp[board[i+1][j]] : 0)+ (i+1<m && j+1<n ? mp[board[i+1][j+1]] : 0)+(i+1<m && j-1>=0 ? mp[board[i+1][j-1]] : 0); if(board[i][j] && (a==2 || a==3)) continue; else if(board[i][j] && a<2) board[i][j]=3; else if(board[i][j] && a>3) board[i][j]=3; else if(!board[i][j] && a==3) board[i][j]=2; } } mp[2]=1; mp[3]=0; for(i=0; i<m; i++){ for(j=0; j<n; j++) board[i][j]=mp[board[i][j]]; } return; } };
Problem solution in C.
#define DEAD_TO_ALIVE 2 #define ALIVE_TO_DEAD 3 void getStatus(int i, int j, int** board, int boardSize, int boardColSize, int* alive) { if ((i < 0) || (j < 0) || (i == boardSize) || (j == boardColSize)) { return; } if ((board[i][j] == 1) || (board[i][j] == ALIVE_TO_DEAD)) { (*alive)++; } } void gameOfLife(int** board, int boardSize, int* boardColSize){ for (int i = 0; i < boardSize; i++) { for (int j = 0; j < *boardColSize; j++) { int adjAlive = 0; getStatus(i - 1, j - 1, board, boardSize, *boardColSize, &adjAlive); getStatus(i, j - 1, board, boardSize, *boardColSize, &adjAlive); getStatus(i + 1, j - 1, board, boardSize, *boardColSize, &adjAlive); getStatus(i - 1, j, board, boardSize, *boardColSize, &adjAlive); getStatus(i + 1, j, board, boardSize, *boardColSize, &adjAlive); getStatus(i - 1, j + 1, board, boardSize, *boardColSize, &adjAlive); getStatus(i, j + 1, board, boardSize, *boardColSize, &adjAlive); getStatus(i + 1, j + 1, board, boardSize, *boardColSize, &adjAlive); if (board[i][j] == 0) { if (adjAlive == 3) { board[i][j] = DEAD_TO_ALIVE; } } else { if (adjAlive < 2 || adjAlive > 3) { board[i][j] = ALIVE_TO_DEAD; } } } } for (int i = 0; i < boardSize; i++) { for (int j = 0; j < *boardColSize; j++) { if (board[i][j] == DEAD_TO_ALIVE) { board[i][j] = 1; } else if (board[i][j] == ALIVE_TO_DEAD) { board[i][j] = 0; } } } }
0 Comments