codeforce 24D Broken robot—概率DP—conquersea博客

本文通过一道关于机器人行走路径期望的题目,详细介绍了如何运用概率动态规划(概率DP)解决问题的方法,包括逆推求期望的过程及代码实现。
                                                                                                                     D. Broken robot
                                                                                                                           time limit per test 2 seconds
                                                                                                                     memory limit per test 256 megabytes
                                                                                                                           input standard input
                                                                                                                          output standard output
You received as a gift a very clever robot walking on a rectangular board. Unfortunately, you understood that it is broken and behaves rather strangely (randomly). The board consists of N rows and Mcolumns of cells. The robot is initially at some cell on the i-th row and the j-th column. Then at every step the robot could go to some another cell. The aim is to go to the bottommost (N-th) row. The robot can stay at it's current cell, move to the left, move to the right, or move to the cell below the current. If the robot is in the leftmost column it cannot move to the left, and if it is in the rightmost column it cannot move to the right. At every step all possible moves are equally probable. Return the expected number of step to reach the bottommost row.

Input
On the first line you will be given two space separated integers N andM (1 ≤ N, M ≤ 1000). On the second line you will be given another two space separated integers i and j (1 ≤ i ≤ N, 1 ≤ j ≤ M) — the number of the initial row and the number of the initial column. Note that, (1, 1) is the upper left corner of the board and (N, M) is the bottom right corner.
Output

Output the expected number of steps on a line of itself with at least 4digits after the decimal point.


Examples
input
10 10
10 4
output
0.0000000000
input
10 14
5 14
output

18.0038068653

这是一道概率DP,作为一名概率初学者,没有搜到关于这道概率DP相关的解释,于是我想发表一下我的见解。

题意—机器人从x,y处走到最下面一行,求步数的期望,机器人可以随机向左右下方走动,但不能过左右边界,也可能停在原地,这几种可能概率相等。


首先概率DP题目,我在kuanbin的博客上看到一个结论,求概率正推,求期望逆推,这一题是求期望,所以需要逆推。

那么建一个mapp的二维数组,里面保存从i行j列到最后一行的期望。然后从最后一行慢慢往上算。到达x行停止。


这里面有一个大坑点,停在原地也算一步(=_=),个人理解“步”,有点偏差。


推导公式,因为确定位置的期望是一定的,设mapp[i][j]处期望为a.它由周围的mapp值决定。以中间某个一般值解释(代码else里面情况)

a=mapp[i+1][j]*1/4+mapp[i][j+1]*1/4+mapp[i][j-1]*1/4+a*1/4+1;

变形得a=(mapp[i+1][j]*1/4+mapp[i][j+1]*1/4+mapp[i][j-1]*1/4+1)/(1-1/4);


比较难懂的就是t循环是什么,笼统的说是减小误差(t=1开始时误差非常大);

t的作用是体现在一整行上面,因为一行的某个值左右没有确定自己也不能确定,比如t=1时,求i行1列的期望其实只求了向下走一步这种可能,求i行2列期望时由于1列已经求好

了,2列的期望保存的是向下和左转向下2种可能,比第一列精确了。

然后循环t=2,更新此行1列的值,保存的就是向下,向右向下,向右向左向下三中可能。以此类推.........机器人在同一行回荡步数越多概率越小,回荡100步可能几乎为0,t=100就十分精确了。此时此行各列保存了,机器人以各种方式走到下一行的概率和。这个在定义上就是期望了。

解释的很累,我做为初学概率DP的做题时也很难理解。

#include<iostream>
#include<cstdio>
using namespace std;
double mapp[1005][1005];
int n,m,x,y;
int main()
{
    cin>>n>>m>>x>>y;
    for(int i=n-1;i>=x;i--)
    {
        for(int t=1;t<=100;t++)
        {
            for(int j=1;j<=m;j++)
            {
                if(m==1)
                mapp[i][j]=(mapp[i+1][j]*1/2.0+1)*2;
                else
                {
                   if(j==1)
                        mapp[i][j]=(mapp[i+1][j]*1/3.0+mapp[i][j+1]*1/3.0+1)/(1-1/3.0);
                    else if(j==m)
                        mapp[i][j]=(mapp[i+1][j]*1/3.0+mapp[i][j-1]*1/3.0+1)/(1-1/3.0);
                    else
                        mapp[i][j]=(mapp[i+1][j]*1/4.0+mapp[i][j+1]*1/4.0+mapp[i][j-1]*1/4.0+1)/(1-1/4.0);
                }
            }
        }
    }
    printf("%.6f",mapp[x][y]);
    return 0;
}


### Codeforces 题目难度分布及评级标准 Codeforces 的题目难度范围广泛,涵盖了从新手到专家级别的各种挑战[^1]。该平台通过细致的分层机制来区分不同难度等级的问题,使得每位参赛者都能找到适合自己水平的任务。 #### 评分体系概述 Codeforces 使用基于积分制的评价系统,其中每道题都有一个对应的分数(rating),用于表示其相对难易程度。较低的 rating 表明这是一道较为简单的题目;而较高的 rating 则意味着更高的复杂性和解决难度。通常情况下: - **简单题**:Rating 小于等于 1200 分 - **中等偏难题**:Rating 范围大约在 1600 至 2000 分之间 - **困难题**:Rating 大于等于 2400 分 这些数值并不是固定的界限,而是根据社区反馈以及比赛实际情况调整的结果。 #### Divisions 和 Contest Types 为了更好地适应不同程度的学习者和技术爱好者的需求,Codeforces 提供了多种类型的竞赛活动,比如 Division 1, Division 2, Division 3 及 Division 4 等不同类型的比赛[^2]。每个 division 对应着不同的最低准入门槛——即参与者应该具备的基础能力或经验水平。例如,在更高级别的比赛中会遇到更多高难度的问题。 #### 技术领域覆盖 平台上发布的题目不仅限于单一的技术方向,还涉及到多个计算机科学的重要分支,如贪心算法、动态规划、图论等领域。这种多样性有助于全面锻炼编程技巧并促进跨学科思维的发展。 ```python # 示例 Python 代码片段展示如何获取某场比赛的信息 import requests def get_contest_info(contest_id): url = f"https://codeforces.com/api/contest.standings?contestId={contest_id}&from=1&count=1" response = requests.get(url).json() if 'result' not in response or 'problems' not in response['result']: return None problems = response['result']['problems'] for problem in problems: print(f"{problem['index']}: {problem['name']} - Rating: {problem.get('rating', 'N/A')}") get_contest_info(1669) ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值