# Leetcode-game of Life

Source: Internet
Author: User

According to the Wikipedia's article: "The Gameof Life, also known simply as Life , is a cellular automa ton devised by the British mathematician John Horton Conway in 1970. "

Given a board with m - n cells, each cell have 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 t He above Wikipedia article):

1. Any live cell with fewer than-live neighbors dies, as if caused by under-population.
2. Any live cell with a or three live neighbors 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 cells 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 it current state.

Followup:

1. Could You solve it in-place? Remember the board needs to be updated at the same time:you cannot update some cells first and then use their update D values to update the other cells.
2. In this question, we represent the board using a 2D array. In principle, the board was infinite, which would cause problems when the active area encroaches the border of the array. How would address these problems?

Credits:
Special thanks to @jianchao. Li.fighter for adding the problem and creating all test cases.

Analysis:we need A-encode a position ' s good neighbors and also its existing status:dead:-(# of Live neighbors)-1  Live: (# of Live neighbors) +1note:the-1 and +1 are used to seperate the states with 0 live neighbors. Another calculate the next status In-place and use 2 bits coding,00:dead but would live01:dead and would die10: Live but would die11:live and would live solution:
` Public classSolution { Public voidGameoflife (int[] board) {    if(board = =NULL|| Board.length = = 0)return; intm = board.length, n = board[0].length;  for(inti = 0; I < m; i++) {         for(intj = 0; J < N; J + +) {            intlives =liveneighbors (board, M, N, I, j); //in the beginning, every 2nd bit is 0; //So we are only need to care on when the 2nd bit is become 1.            if(Board[i][j] = = 1 && lives >= 2 && lives <= 3) {Board[i][j]= 3;//Make the 2nd bit 1:01--->            }            if(Board[i][j] = = 0 && lives = = 3) {Board[i][j]= 2;//Make the 2nd bit--->            }        }    }     for(inti = 0; I < m; i++) {         for(intj = 0; J < N; J + +) {Board[i][j]>>= 1;//Get the 2nd state.        }    }} Public intLiveneighbors (int[] board,intMintNintIintj) {intlives = 0;  for(intx = Math.max (i-1, 0); X <= math.min (i + 1, m-1); X + +) {         for(inty = Math.max (j-1, 0); Y <= Math.min (j + 1, n-1); y++) {lives+ = Board[x][y] & 1; }} Lives-= Board[i][j] & 1; returnlives;}}`

Leetcode-game of Life

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.