zoukankan      html  css  js  c++  java
  • 289. Game of Life

    package LeetCode_289
    
    /**
     * 289. Game of Life
     * https://leetcode.com/problems/game-of-life/
     * 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):
    1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.
    2. Any live cell with two or three live neighbors lives on to the next generation.
    3. Any live cell with more than three live neighbors dies, as if by over-population.
    4. 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.
    The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously.
    Example:
    Input:
    [
    [0,1,0],
    [0,0,1],
    [1,1,1],
    [0,0,0]
    ]
    Output:
    [
    [0,0,0],
    [1,0,1],
    [0,1,1],
    [0,1,0]
    ]
     * */
    class Solution {
        /*
        * solution: simulation, scan board:
        * 1. if need set die to live, mark current position to -1,
        * 2. if need set live to die, mark current position to 2,
        * Time: O(m*n), Space: O(1)
        * */
        val directions = arrayOf(
            //x,y
            intArrayOf(0, 1),//up
            intArrayOf(1, 0),//right
            intArrayOf(0, -1),//down
            intArrayOf(-1, 0),//left
            intArrayOf(-1, 1),//up,left
            intArrayOf(1, 1),//up,right
            intArrayOf(-1, -1),//down,left
            intArrayOf(1, -1)//down,right
        )
    
        fun gameOfLife(board: Array<IntArray>): Unit {
            val m = board.size
            val n = board[0].size
            for (i in 0 until m) {
                for (j in 0 until n) {
                    //need set to live: Any dead cell with exactly three live neighbors becomes a live cell
                    if (board[i][j] == 0) {
                        val liveCount = countLive(board, i, j)
                        if (liveCount == 3) {
                            board[i][j] = -1
                        }
                    }
                    //need set to die:
                    //1. Any live cell with more than three live neighbors dies
                    //2. Any live cell with fewer than two live neighbors dies
                    if (board[i][j] == 1) {
                        val liveCount = countLive(board, i, j)
                        if (liveCount > 3 || liveCount < 2) {
                            board[i][j] = 2
                        }
                    }
                }
            }
            update(board)
        }
    
        private fun countLive(board: Array<IntArray>, x: Int, y: Int): Int {
            var count = 0
            for (dir in directions) {
                val newX = x + dir[0]
                val newY = y + dir[1]
                if (newX < 0 || newY < 0 || newX >= board.size || newY >= board[0].size) {
                    continue
                }
                if (board[newX][newY] == 1 || board[newX][newY] == 2) {
                    count++
                }
            }
            return count
        }
    
        private fun update(board: Array<IntArray>) {
            val m = board.size
            val n = board[0].size
            for (i in 0 until m) {
                for (j in 0 until n) {
                    if (board[i][j] == -1) {
                        board[i][j] = 1
                    }
                    if (board[i][j] == 2) {
                        board[i][j] = 0
                    }
                }
            }
        }
    }
  • 相关阅读:
    tp5 宝塔open_basedir restriction in effect 错误; IIS open_basedir restriction in effect
    如何封装一个自己的win7系统并安装到电脑做成双系统
    推荐7个模板代码和其他游戏源码下载的网址
    PHP简单实现异步多文件上传并使用Postman测试提交图片
    PHP公众号开发给用户发微信消息提醒功能
    解决在页面中无法获取qrcode.js生成的base64的图片
    如何使用GUID硬盘分区格式安装新windows系统
    超详细的纯净windows系统重装示例
    Spark之join、leftOuterJoin、rightOuterJoin及fullOuterJoin
    Spark中groupByKey、reduceByKey与sortByKey
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/14155084.html
Copyright © 2011-2022 走看看