题目
题目描述
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 N rows and M M columns of cells. The robot is initially at some cell on the i i -th row and the j j -th column. Then at every step the robot could go to some another cell. The aim is to go to the bottommost ( N 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.
输入格式
On the first line you will be given two space separated integers N N and M M ( 1<=N,M<=1000 1<=N,M<=1000 ). On the second line you will be given another two space separated integers i i and j j ( 1<=i<=N,1<=j<=M 1<=i<=N,1<=j<=M ) — the number of the initial row and the number of the initial column. Note that, (1,1) (1,1) is the upper left corner of the board and (N,M) (N,M) is the bottom right corner.
输出格式
Output the expected number of steps on a line of itself with at least 4 4 digits after the decimal point.
题意翻译
题意翻译
nn 行 mm 列的矩阵,现在在 (i,j)(i,j),每次等概率向左,右,下走或原地不动,但不能走出去,问走到最后一行期望的步数。
注意,(1,1)(1,1) 是木板的左上角,(n,m)(n,m) 是木板的右下角。
输入输出格式
输入格式
第一行为两个整数 n,mn,m。
第二行为两个整数 x,yx,y。
输出格式
一行,输出所需移动步数的数学期望值保留 44 位小数的值。
说明/提示
1\le n,m\le 10^31≤n,m≤10
3
,1\le x\le n1≤x≤n,1\le y\le m1≤y≤m。
输入输出样例
输入 #1 复制
10 10
10 4
输出 #1 复制
0.0000000000
输入 #2 复制
10 14
5 14
输出 #2 复制
18.0038068653
思路
我们考虑到不好处理的是一个反复横走的情况。我们在这里对每一行进行多次暴力的转移(希望能够包含在这一行多次横跳产生的期望),就是从所有可能方向走进来(包括自己),然后计算期望,也不用什么抹除贡献啥的,反正就是模拟+暴力。
考虑到在一行反复横跳 100 次的概率是极小的,因此我们这样基本上是可以保证正确的。
代码
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1100;
int n,m,a,b,d[MAXN];
double x[MAXN],y[MAXN],*now,*last;
int main(){
scanf("%d %d %d %d",&n,&m,&a,&b);
if(a == n){printf("0\n");return 0;}
n -= a;
for(int i = 1;i<=m;i++){// 可能走进来的方案数
d[i] = 2;
if(i != 1) d[i]++;
if(i != m) d[i]++;
}
now = x,last = y;
for(int i = 1;i<=n;i++){
for(int j = 1;j<=m;j++) now[j] = 0;
for(int c = 1;c<=100;c++){//多次模拟
for(int j = 1;j<=m;j++){
now[j] = (now[j-1]+now[j]+now[j+1]+last[j])/d[j]+1;
}
}
swap(now,last);
}
printf("%lf\n",last[b]);
return 0;
}