国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[Leetcode] Game of Life 生命游戲

android_c / 818人閱讀

摘要:思路普通解法,遍歷每一個細胞求值,用一個的矩陣存放結果。求值過程,稍微分析一下可知,其實就是按照以下的矩陣進行結果是可數的。

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?

思路1
普通解法,遍歷每一個細胞求值,用一個M*N的矩陣存放結果。
求值過程,稍微分析一下可知,其實就是按照以下的矩陣進行,結果是可數的。

        細胞狀態       0    1
        鄰居1個數0     0    0
                1     0    0
                2*    0    1
                3*    1    1
                4     0    0
                5     0    0
                6     0    0
                7     0    0
                8     0    0

復雜度
空間(MN),時間(MN)

實現代碼1

public class GameofLife {
    
    /**
     * 直接解法    
     * 空間(n)
     * @param board
     * @return
     */
    public int[][] Solution1(int[][] board){
        int m = board.length;s

        int n = board[0].length;

        int[][] result = new int [m][n];

        for(int i = 0; i= MAXROW || c < 0 || c >= MAXCOL)  
                        continue;  
                    if(r==row && c == col)
                        continue;
                    if(board[r][c] == 1)  
                        count++;  
                }  
         if(count==3)
             result = 1;
         else if(board[row][col] == 1 && count ==2)
             result = 1;
         else 
             result = 0;
        
        return result;
    }
}

思路2
要求inplace解法,即不用額外的空間,那么就要同時保存細胞 前后的狀態
這個解法用0,1,2,3來表示細胞下一代的計算結果,計算下一代時(統計鄰居和計算下一個狀態),把這四種情況都考慮進去,
最后矩陣%2得到最終結果。
0: 0->0
1: 1->1
2: 1->0
3: 0->1

復雜度
空間(1),時間(M*N)

實現代碼2

public class GameofLife {
    /**
     *     0 : 上一輪是0,這一輪過后還是0
        1 : 上一輪是1,這一輪過后還是1
        2 : 上一輪是1,這一輪過后變為0
        3 : 上一輪是0,這一輪過后變為1
     * @param board
     * @return
     */
    public int[][] Solution2(int[][] board){
        int m = board.length;

        int n = board[0].length;

        int[][] result = new int [m][n];

        for(int i = 0; i= MAXROW || c < 0 || c >= MAXCOL)  
                        continue;  
                    if(r==row && c == col)
                        continue;
                    if(board[r][c] == 1 || board[r][c] == 2)  
                        count++;  
                }  
         
         if(board[row][col] == 1 && (count ==2 || count ==3))
             result = 2;
         else if  (board[row][col]==0 && count == 3) 
             result = 3;
         else
             result = board[row][col];
        
        return result;
    }
}

測試代碼

public class GameofLifeTest {

    private GameofLife s;
    
    @Before
        public void setUp() {
         s = new GameofLife();
        }
     
    @Test
    public void testSolution1() {
        
        int[][] nums = {
                {1,0,1},
                {1,0,1},
                {1,0,1}
        };
        int[][] expect = {
                {1,0,1},
                {1,0,1},
                {1,0,1}
        };
        
        int[][] result = s.Solution1(nums);
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++)
                System.out.print(result[i][j]);
            System.out.println("");
        }        
    }
    
    @Test
    public void testSolution2() {
        
        int[][] nums = {
                {1,0,1},
                {1,0,1},
                {1,0,1}
        };
        int[][] expect = {
                {1,0,1},
                {1,0,1},
                {1,0,1}
        };
        
        int[][] result = s.Solution2(nums);
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++)
                System.out.print(result[i][j]);
            System.out.println("");
        }        
    }
    
}

測試結果

000
101
000

101
000
101

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/68446.html

相關文章

  • [Leetcode] Game of Life 生命游戲

    摘要:如果多核的機器如何優化因為是多核,我們可以用線程來實現并行計算。如果線程變多分塊變多,邊緣信息也會變多,開銷會增大。所以選取線程的數量是這個開銷和并行計算能力的折衷。 Game of Life I According to the Wikipedias article: The Game of Life, also known simply as Life, is a cellula...

    XFLY 評論0 收藏0
  • leetcode289. Game of Life

    摘要:板上的每個小格子有兩種狀態,或。而根據游戲規則,每一次這個板上的內容將會隨著前一次板上的內容發生變化。然后再根據當前格子的狀態計算當前格子的下一個狀態。當然最后別忘了將原始狀態傳遞出去。 題目要求 According to the Wikipedias article: The Game of Life, also known simply as Life, is a cellular...

    jerryloveemily 評論0 收藏0
  • Python+Pygame實操之玩命吃水果游戲的完成

      吃豆人和削蘋果這兩個游戲想必大家都知道吧,本文運用Python里的Pygame控制模塊編寫出一個融合吃豆人+切水果的新手游:玩命吃蘋果,有興趣的話可以認識一下  引言  哈哈哈!木木子今天浮現——早已來給大家看了不少具體內容啦~  涉及到的人工智能、新手、網絡爬蟲、數據統計分析(這一塊的通常但是審批)手機游戲...  PS:  吃豆人我寫過了哈  Python+Pygame實戰之吃豆豆游戲的實...

    89542767 評論0 收藏0
  • 康威生命游戲的簡單實現

    摘要:生命游戲,數學家發明的一個游戲,又稱康威生命演化,生命棋,細胞自動機。康威有許多好玩有趣的發明,最廣為人知的一個是外觀數列,這里不多說,另一個就是生命游戲。生命游戲模擬的是二維平面上生命的演化過程。 生命游戲,數學家 John Conway 發明的一個游戲,又稱康威生命演化,生命棋,細胞自動機。 康威有許多好玩有趣的發明,最廣為人知的一個是外觀數列(Look-and-Say),這里不多...

    ccj659 評論0 收藏0
  • [LeetCode] 289. Game of Life

    Problem According to the Wikipedias 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 ...

    Ajian 評論0 收藏0

發表評論

0條評論

最新活動
閱讀需要支付1元查看
<