zoukankan      html  css  js  c++  java
  • leetcode289

    public class Solution
        {
            public void GameOfLife(int[][] board)
            {
                var row = board.GetLength(0) - 1;
                var col = board[0].GetLength(0) - 1;
                var list = new List<List<int>>();
                for (int r = 0; r <= row; r++)
                {
                    var l = new List<int>();
                    for (int c = 0; c <= col; c++)
                    {
                        l.Add(board[r][c]);
                    }
                    list.Add(l);
                }
    
                for (int r = 0; r <= row; r++)
                {
                    for (int c = 0; c <= col; c++)
                    {
                        int livecount = 0;
                        for (int i = -1; i <= 1; i++)
                        {
                            for (int j = -1; j <= 1; j++)
                            {
                                if (i == 0 && j == 0)
                                {
                                    continue;
                                }
                                var newx = r + i;
                                var newy = c + j;
                                if (newx < 0 || newx > row || newy < 0 || newy > col)
                                {
                                    continue;
                                }
                                if (board[newx][newy] == 1)
                                {
                                    livecount++;//活节点
                                }
                            }
                        }
                        if (board[r][c] == 1)//当前节点是活节点
                        {
                            if (livecount == 2 || livecount == 3)
                            {
                                list[r][c] = 1;
                            }
                            else
                            {
                                list[r][c] = 0;
                            }
                        }
                        else
                        {
                            if (livecount == 3)
                            {
                                list[r][c] = 1;
                            }
                        }
                    }
                }
    
                for (int i = 0; i < list.Count; i++)
                {
                    for (int j = 0; j < list[0].Count; j++)
                    {
                        board[i][j] = list[i][j];
                    }
                }
            }
        }
  • 相关阅读:
    数数小木块
    猴子吃桃问题
    整除个数
    大小写互换
    车牌号
    比较字母大小
    队花的烦恼一
    字母小游戏
    字符串逆序输出
    茵茵的第一课
  • 原文地址:https://www.cnblogs.com/asenyang/p/9741914.html
Copyright © 2011-2022 走看看