You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

笛卡尔平面顶点移动概率编程问题:现有实现仅部分正确求修正

平台顶点路径到达概率计算问题

问题说明

平台包含5个顶点,坐标分别为(-1,0)、(0,-1)、(0,0)、(0,1)、(1,0)。从起点(xs, ys)出发,每次随机选择左(x减1)、右(x加1)、上、下四个方向之一移动,每个方向概率均等且独立。需要计算在掉落平台之前到达终点(xe, ye)的概率。

约束条件:

  • 起点(xs, ys)和终点(xe, ye)均属于上述5个顶点集合
  • 起点与终点不同

现有代码问题

我实现了如下Python函数,但仅部分测试案例返回正确结果,其余全部失败。求帮忙找出问题并修正:

def calculate_probability(xs, ys, xe, ye):
    edges = [[-1, 0], [0, -1], [0, 1], [1, 0]]
    if [xs, ys] in edges:
        if xe == 0 and ye == 0:
            return 0.25
        elif xs == xe and ys == ye:
            return 1.0
        elif [xe, ye] in edges:
            return 0.075
    
    if xs == 0 and ys == 0:
        if [xe, ye] in edges:
            return 0.3
        elif xe == 0 and ye == 0:
            return 1
    return 0

内容的提问来源于stack exchange,提问作者Legendary_Hunter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 03:42:02