美文网首页
game of life

game of life

作者: robertzhai | 来源:发表于2023-07-21 12:13 被阅读0次
func gameOfLife(board [][]int)  {

    /*

    00  0
    01  1
    10  2
    11  3

    */

    n := len(board)
    m := len(board[0])
    cnt := 0
    curX,curY := 0,0
    dx := [8]int{-1,-1,0,1,1,1,0,-1}
    dy := [8]int{0, 1, 1,1,0,-1,-1,-1}
    for i:=0;i<n;i++ {
        for j:=0;j<m;j++ {
            cnt = 0
            for k:=0;k<8;k++ {
                curX = i+dx[k]
                curY = j+dy[k]
                if curX >=0 && curX <n && curY>=0 && curY<m && board[curX][curY] & 1 == 1 {
                    cnt++
                }
            }
            if board[i][j] & 1 == 1 {
                if cnt == 2 || cnt==3 {
                    board[i][j] = 3
                }
            } else {
                if cnt == 3 {
                    board[i][j] = 2
                }
            }
        }
    }

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

}


func gameOfLifeV0(board [][]int)  {

    n := len(board)
    m := len(board[0])
    ret := make([][]int,n)
    for i:=0;i<n;i++ {
        ret[i] = make([]int,m)
    }
    dx := [8]int{-1,-1,0,1,1,1,0,-1}
    dy := [8]int{0, 1, 1,1,0,-1,-1,-1}
    for i:=0;i<n;i++ {
        for j:=0;j<m;j++ {
            cnt := 0
            for k:=0;k<8;k++ {
                curX := i+dx[k]
                curY := j+dy[k]
                if curX >=0 && curX <n && curY>=0 && curY<m && board[curX][curY] & 1 == 1 {
                    cnt++
                }
            }
            if board[i][j] & 1 == 1 {
                if cnt == 2 || cnt==3 {
                    ret[i][j] = 1
                }
            } else {
                if cnt == 3 {
                    ret[i][j] = 1
                }
            }
        }
    }

    for i := 0;i<n;i++ {
        for j:=0;j<m;j++ {
            board[i][j] = ret[i][j]
        }
    }

}

相关文章

  • Life is a game. This is your str

    Life is a game. This is your strategy guide Real life is ...

  • 青隽

    All life is a game of luck

  • NO GAME NO LIFE

    正如那部热门的动漫番剧一样,《NO GAME NO LIFE》就是我的人生信条。 当我将身边的所有事物都附上游戏中...

  • Game of Life

    题目来源一道状态转移的题目,题目比较长…然后实际做起来并不难。我一开始做利用了mn的空间,然后处理起来比较冗长。代...

  • NO GAME NO LIFE

    最强夫妻与最弱兄妹 有一对夫妻他们很强但却从未赢过一次! 【第一】 不能杀死任何人 因为内心不想杀死任何人 【第...

  • No Game No Life

    今儿聊聊游戏,这话题其实有点大,算是一个专题的开篇吧。想到哪个游戏就说哪个,并不会遵循时间顺序。就先从山口山(魔兽...

  • Game episode 1

    Can life be a game? Do u think so? For me , life can be a...

  • 289. Game of Life

    289. Game of Life 题目:https://leetcode.com/problems/game-o...

  • 289. Game of Life

    According to the Wikipedia's article: "The Game of Life, ...

  • 人生成长必须面对的十大残酷事实

    In the game of life, if it often seems like you’re on the...

网友评论

      本文标题:game of life

      本文链接:https://www.haomeiwen.com/subject/osdtpdtx.html