The number of steps 概率dp

Problem Description

    Mary stands in a strange maze, the maze looks like a triangle(the first layer have one room,the second layer have two rooms,the third layer have three rooms …). Now she stands at the top point(the first layer), and the KEY of this maze is in the lowest layer’s leftmost room. Known that each room can only access to its left room and lower left and lower right rooms .If a room doesn’t have its left room, the probability of going to the lower left room and lower right room are a and b (a + b = 1 ). If a room only has it’s left room, the probability of going to the room is 1. If a room has its lower left, lower right rooms and its left room, the probability of going to each room are c, d, e (c + d + e = 1). Now , Mary wants to know how many steps she needs to reach the KEY. Dear friend, can you tell Mary the expected number of steps required to reach the KEY?


Input
There are no more than 70 test cases. 
 
In each case , first Input a positive integer n(0
The input is terminated with 0. This test case is not to be processed.
Output
Please calculate the expected number of steps required to reach the KEY room, there are 2 digits after the decimal point.
Sample Input
3
0.3 0.7
0.1 0.3 0.6
0 
Sample Output

3.41



阶梯型迷宫。

从1,1到n,1的概率。

每个房间最多有三个们。左,左下,右下。

如果只有左门,则概率为1.

只有左下右下,a,b

同时有三个,c,d,e。

求概率。

反向思考,我们从(n,1)往(1,1)走。

我们的数组是这样列的,一定要注意

(1,1)

(2,1)(2,2)

(3,1)(3,2)(3,3)

下面的三个步骤的走法不要搞糊涂了。我们左下角是n,1.

①对于第n行的房间我们怎么走。

对于(n,i),我们只能从(n,i-1)到达,也就是概率为1.

②对于第1列的房间(i,1)

我们只有两种进入方法,从(i+1,2)(i,2)到达。

③对于其它的(i,j)。

我们可以从(i+1,j)(i+1,j+1)(i,j-1)到达。  

所以利用dp从n行到1行,从1列到i列枚举即可。



#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
double mp[110][110];
double a,b,c,d,e;
int n;
int main()
{
    while(cin>>n&&n!=0)
    {
        memset(mp,0,sizeof(mp));
        cin>>a>>b>>c>>d>>e;
        for(int i=n;i>=0;i--)
        {
            for(int j=1;j<=i;j++)
            {
                if(i==n&&j!=1)//只能往左走的,概率为1.也就是最后一行,我们只能往左
                {
                   mp[i][j]=mp[i][j-1]+1;
                }
                if(j==1&&i!=n)//第一列的可以由左下和右下到达
                {
                    mp[i][j]=a*(mp[i+1][j]+1)+b*(mp[i+1][j+1]+1);
                }
                if(i!=n&&j!=1)//其他位置的点,都有三种到达方式
                {
                     mp[i][j]=d*(mp[i+1][j+1]+1)+e*(mp[i][j-1]+1)+c*(mp[i+1][j]+1);
                }
            }
        }
        printf("%0.2f\n",mp[1][1]);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值