zoukankan      html  css  js  c++  java
  • leetcode 289 生命游戏

    原题点这里

    这个是每日一题里面的,所以顺序有点混乱

    是个模拟的水题

    public static int check(int[][]board,int row,int col){
            int[] dx = new int[]{-1,0,1};
            int[] dy = new int[]{-1,0,1};
            int liveNum=0;
            int deadNum=0;
            int rows = board.length;
            int cols = board[0].length;
            for(int i=0;i<3;i++){
                for(int j=0;j<3;j++){
                    if(dx[i]==0&&dy[j]==0) continue;
                    int x = row+dx[i];
                    int y = col+dy[j];
                    if(x>=0&&y>=0&&x<rows&&y<cols){
                        if(board[x][y]==0) deadNum++;
                        if(board[x][y]==1) liveNum++;
                    }
                }
            }
            if(liveNum<2) return 0;
            if(liveNum==2) return board[row][col];
            if(liveNum==3) return 1;
            return 0;
        }
        public static void gameOfLife(int[][] board) {
            int row = board.length;
            int col = board[0].length;
            int [][] newBoard = new int [row][col];
            for(int i=0;i<row;i++){
                for(int j=0;j<col;j++)
                    newBoard[i][j]=board[i][j];
            }
            for(int i=0;i<row;i++){
                for(int j=0;j<col;j++){
                    board[i][j]=check(newBoard,i,j);
                }
            }
    
        }
    View Code
  • 相关阅读:
    Ubuntu 17 安装sublime
    ubuntu17 设置python3为默认及一些库的安装
    Java中内存分析(一)
    我的学习JavaEE路线
    我爱学习……
    HDU 4602
    K-special Tables
    Gym 100712A - Who Is The Winner
    UVA 1583
    水题 UVA 1586
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/12622712.html
Copyright © 2011-2022 走看看