zoukankan      html  css  js  c++  java
  • 移动后存活概率

    问题见图片

    def how_likely_alive(size, startx, starty, n):
        sum_probability = 4 ** n         # 所有可能的移动结果数
        start_point = [(startx, starty)] # 定义起始位置坐标
    
        while n > 0:
            end_point = []         # 定义空列表,存放移动后还存活着的坐标
            for (x, y) in start_point:
                if 0 <= x + 1 <= size:  # 前移,并存活
                    end_point.append((x + 1, y))
                if 0 <= x - 1 <= size:  # 后移,并存活
                    end_point.append((x - 1, y))
                if 0 <= y + 1 <= size:  # 上移,并存活
                    end_point.append((x, y + 1))
                if 0 <= y - 1 <= size:  # 下移,并存活
                    end_point.append((x, y - 1))
            n -= 1              # 移动后,步数n-1
            start_point = end_point.copy()# 移动后更新可能的起始坐标点
        alive_number = len(end_point)    
        alive_probability = alive_number / sum_probability
        return alive_probability
  • 相关阅读:
    模线性方程理解
    dp水题
    静态字典树模板
    KMPnext数组循环节理解 HDU1358
    layer开发随笔
    javascript时间格式转换
    ubuntu16创建开机启动服务
    es集群搭建
    mongodb集群搭建
    zookeeper集群搭建
  • 原文地址:https://www.cnblogs.com/fansirs/p/13474782.html
Copyright © 2011-2022 走看看