您的位置:首页 > Web前端

【LeetCode】Game of Life

2015-10-14 10:48 323 查看
Game of Life

My Submissions Question Solution 

Total Accepted: 3524 Total Submissions: 11205 Difficulty: 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?

Credits:

Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

【解题思路】

1、这个题目的难点在于,每次既要修改当前状态,又要保存当前状态。

2、初始状态为0和1,那么变化后的状态无非有四个,{00,01,10,11},其中高位表示下个状态,低位表示当前状态。

3、根据题目要求:

      1)、当前状态为1,活细胞少于2个,那么它就死亡了,变为01,即数字1,其实不需要改变当前值。

      2)、当前状态为1,活细胞为2或者3,它继续生存,变为11,即数字3。

      3)、当前状态为1,活细胞多余3个,死亡,变为01,还是1,不需要改变当前值。

      4)、当前状态为0,活细胞为3个,它变为活细胞,状态为10,即数字2。

4、这样统计每个细胞周围活细胞的数量,根据3的描述,值为1和3的都是活着的。

5、对每个细胞做一次这样的操作,最后右移一位即为下一个状态。

Java AC

public class Solution {
public void gameOfLife(int[][] board) {
int m = board.length;
int n = board[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int status = getStatus(board, m, n, i, j);
if ((board[i][j] == 1 || board[i][j] == 3)
&& (status == 2 || status == 3)) {
board[i][j] = 3;
} else {
board[i][j] = status == 3 ? 2 : board[i][j];
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
board[i][j] >>= 1;
}
}
}

private int getStatus(int[][] board, int m, int n, int x, int y) {
int live = 0;
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
int newX = x + i;
int newY = y + j;
if (newX == x && newY == y) {
continue;
}
if (newX >= 0 && newY >= 0 && newX < m && newY < n) {
if (board[newX][newY] == 1 || board[newX][newY] == 3) {
live++;
}
}
}
}
return live;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: