资讯专栏INFORMATION COLUMN

[Leetcode] Game of Life 生命游戏

android_c / 636人阅读

摘要:思路普通解法,遍历每一个细胞求值,用一个的矩阵存放结果。求值过程,稍微分析一下可知,其实就是按照以下的矩阵进行结果是可数的。

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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/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元查看
<