[LeetCode]289. Game of Life

本文介绍了一种基于康威生命游戏规则的算法实现方法。该算法通过计算每个细胞周围活细胞的数量来决定其生死状态,并据此更新整个游戏板的状态。文章提供了两种不同的Java实现方案,包括状态位操作和邻居计数的方法。

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?

1ms:

public void gameOfLife(int[][] board) {
    final int L2D = 1;
    final int L2L = 3;
    final int D2L = 2;
    final int D2D = 0;

    int m = board.length;
    int n = board[0].length;

    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
//          System.out.println("i="+i+",j="+j);
            int count = 0;
            int status = board[i][j]&1;
//          System.out.println("status="+status);
            count+=getR(i,j,m,'m',board)&1;
            count+=getR(i,j,m,'p',board)&1;
            count+=getC(i,j,n,'m',board)&1;
            count+=getC(i,j,n,'p',board)&1;
            count+=getD(i-1,j,m,n,'m',board)&1;
            count+=getD(i-1,j,m,n,'p',board)&1;
            count+=getD(i+1,j,m,n,'m',board)&1;
            count+=getD(i+1,j,m,n,'p',board)&1;
//          System.out.println("count="+count);
            if(status==1){
                if(count<4&&count>1)
                    board[i][j]=L2L;
                else 
                    board[i][j]=L2D;
            }else{
                if(count==3)
                    board[i][j]=D2L;
                else
                    board[i][j]=D2D;
            }
        }
    }

    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            board[i][j]=board[i][j]>>1;
        }
    }
}

int getC(int i,int j,int max, char x,int[][] board){
    if(x=='m'){
        if(j==0)
            return 0;
        else
            return board[i][j-1];
    }else{
        if(j==max-1)
            return 0;
        else
            return board[i][j+1];
    }
}
int getR(int i,int j,int max,char x,int[][] board){
    if(x=='m'){
        if(i==0)
            return 0;
        else
            return board[i-1][j];
    }else{
        if(i==max-1)
            return 0;
        else
            return board[i+1][j];
    }
}
int getD(int i,int j,int m,int n,char x,int[][] board){
    if(x=='m'){
        if(i<0||j==0||i==m)
            return 0;
        else 
            return board[i][j-1];
    }else{
        if(i<0||j==n-1||i==m)
            return 0;
        else
            return board[i][j+1];
    }

}

1ms:

public void gameOfLife(int[][] board){
        int m = board.length;
        int n = m>0?board[0].length:0;
        if(m*n==0)   return;
//      System.out.println(m+":"+n);
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
//               System.out.println("i:"+i+",j:"+j);
                 int lives = getLives(i,j,m,n,board);
                 if(board[i][j]==1&&lives>1&&lives<4)
                     board[i][j]=3;
                 if(board[i][j]==0&&lives==3)
                     board[i][j]=2;
             }
         }
         for(int i=0;i<m;i++){
             for(int j=0;j<n;j++){
                 board[i][j]=board[i][j]>>1;
             }
         }
}

int getLives(int i,int j,int m, int n,int[][] board){
    int lives = 0;
    for(int a=Math.max(i-1, 0);a<=Math.min(i+1, m-1);a++){
        for(int b=Math.max(j-1, 0);b<=Math.min(j+1, n-1);b++){
//          System.out.println("a:"+a+",b:"+b);
            lives+=board[a][b]&1;
        }
    }
    lives-=board[i][j]&1;
    return lives;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值