LeetCode:Game of Life - 康威生命遊戲

一、題目名稱java

Game of Life(康威生命遊戲)函數

二、題目地址code

https://leetcode.com/problems/game-of-lifethree

三、題目內容遊戲

英文:ip

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."ci

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):leetcode

  1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.開發

  2. Any live cell with two or three live neighbors lives on to the next generation.get

  3. Any live cell with more than three live neighbors dies, as if by over-population..

  4. 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.

中文:

根據維基百科條目 Conway's Game of Life(康威生命遊戲),康威生命遊戲是英國數學家約翰·何頓·康威在1970年發明的細胞自動機。

給出一個m*n的細胞矩陣,每一個細胞都有一個初始狀態:生存(1)或死亡(0)。每一個細胞的變化都與它周圍8個細胞有關,規則以下:

  1. 當前細胞爲存活狀態時,當週圍存活細胞不到2個時, 該細胞變成死亡狀態。(模擬生命數量稀少)

  2. 當前細胞爲存活狀態時,當週圍有2個或3個存活的細胞時, 該細胞保持原樣。

  3. 當前細胞爲存活狀態時,當週圍有3個以上的存活細胞時,該細胞變成死亡狀態。(模擬生命數量過多)

  4. 當前細胞爲死亡狀態時,當週圍剛好有3個存活細胞時,該細胞變成存活狀態。 (模擬繁殖)

寫一個函數,根據矩陣當前的狀態,計算這個細胞矩陣的下一個狀態。

四、解題方法

在不使用定義新矩陣的狀況下,要想解決本問題就須要先定義一組中間狀態,中間狀態要求能看出一個單元格變化先後兩方面的生死狀況。Java代碼以下:

/**
 * @功能說明:LeetCode 289 - Game of Life
 * @開發人員:Tsybius2014
 * @開發時間:2015年10月8日
 */
public class Solution {

    //死亡單位
    final int DEAD = 0;
    //存活單位
    final int ALIVE = 1;

    //變化狀況:死亡→死亡
    final int DEAD_TO_DEAD = 0;
    //變化狀況:存活→存活
    final int ALIVE_TO_ALIVE = 1;
    //變化狀況:存活→死亡
    final int ALIVE_TO_DEAD = 2;
    //變化狀況:死亡→存活
    final int DEAD_TO_ALIVE = 3;
    
    /**
     * 判斷某點在本輪變化前是不是死亡狀態
     * @param obj
     * @return
     */
    private boolean isAliveOld(int obj) {
        if (obj == ALIVE_TO_ALIVE || obj == ALIVE_TO_DEAD) {
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * 判斷某點在本輪變化後是不是死亡狀態
     * @param obj
     * @return
     */
    private boolean isAliveNew(int obj) {
        if (obj % 2 == 1) {
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * 生命遊戲
     * @param board
     */
    public void gameOfLife(int[][] board) {

        //輸入合法性檢查
        if (board == null) {
            return;
        }
        int height = board.length;
        if (height == 0) {
            return;
        }
        int width = board[0].length;
        if (width == 0) {
            return;
        }

        //考察全部的點,變化其生命狀態
        int counter = 0;
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                
                //統計周邊生命生存狀況
                counter = 0;
                if (i > 0 && j > 0 && isAliveOld(board[i - 1][j - 1])) {
                    counter++;
                }
                if (i > 0 && isAliveOld(board[i - 1][j])) {
                    counter++;
                }
                if (i > 0 && j < width - 1 && isAliveOld(board[i - 1][j + 1])) {
                    counter++;
                }
                if (j > 0 && isAliveOld(board[i][j - 1])) {
                    counter++;
                }
                if (j < width - 1 && isAliveOld(board[i][j + 1])) {
                    counter++;
                }
                if (i < height - 1 && j > 0 && isAliveOld(board[i + 1][j - 1])) {
                    counter++;
                }
                if (i < height - 1 && isAliveOld(board[i + 1][j])) {
                    counter++;
                }
                if (i < height - 1 && j < width - 1 && isAliveOld(board[i + 1][j + 1])) {
                    counter++;
                }
                
                //根據指定點周邊的生命生存狀況決定當前點的變化
                if (isAliveOld(board[i][j])) {
                    if (counter < 2) {
                        //1.存活單位周邊的存活單位少於2個,該單位死亡
                        board[i][j] = ALIVE_TO_DEAD;
                    } else if (counter == 2 || counter == 3) {
                        //2.存活單位周邊的存活單位有2-3個,該單位繼續存活
                        board[i][j] = ALIVE_TO_ALIVE;
                    } else {
                        //3.存活單位周邊的存活單位多餘3個,該單位死亡
                        board[i][j] = ALIVE_TO_DEAD;
                    }
                } else {
                    if (counter == 3) {
                        //4.死亡單位周邊的存活單位剛好爲3個,該單位變爲存活狀態
                        board[i][j] = DEAD_TO_ALIVE;                    
                    } else {
                        board[i][j] = DEAD_TO_DEAD;
                    }
                }
            }
        }

        //根據變換後的存活狀態,從新賦予每一個點的生死狀況
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                if (isAliveNew(board[i][j])) {
                    board[i][j] = ALIVE;
                } else {
                    board[i][j] = DEAD;
                }
            }
        }
    }
}

END

相關文章
相關標籤/搜索