第四届省赛题解-The number of steps(概率DP+数学期望)

The number of steps

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<n<45), which means the layer of the maze, then Input five real number a, b, c, d, e. (0<=a,b,c,d,e<=1, a+b=1, c+d+e=1). 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
题意:
Mary在一个迷宫里,这个迷宫形状想一个三角形,第一层有一个的房间,第二层有两个房间,第三层有三个房间,以此类推。现在Mary为位于迷宫的迷宫的最顶层,问她想要走到最低层最左边的要走多少步。Mary往下走的时候可能出现三种情况,第一中情况只能往左走时,往左走的概率为1;第二种情况,只能往左下,右下时,往左下走概率为a和往右下走概率为b;第三种情况,能往左下走,右下走,和左走,往左下走概率为c,往右下走概率为d,往左走概率为e;问从顶部到左下角期望走的步数。
解题思路:
数学期望E(X) = X1*p(X1) + X2*p(X2) + …… + Xn*p(Xn);
比如打靶打中8环的概率为0.3 ,打中7环的概率为0.7,那么打中环数的期望就是 8*0.3 + 7*0.7;
本题中我们用dp[i][j] 表示当前位置(i,j,表示房间的位置,最顶层的房间为(1,1),最低层最左边为(n,1))距离目的地还需要走的期望步数。那么目的地假设为dp[n][1] (根据建的坐标不一样,位置也不一样),那么dp[n][1]的值为0,因为已经到达目的地,不需要再走了。那么我们所求的就是dp[1][1] 开始的地方。所以解题的过程,就是一个逆推的过程。整个逆推过程完成,dp[1][1]内的值就是所求的期望步数。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;

const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const int N=2008;

double a,b,c,d,e;
double dp[100][100];

int main()
{
    int n;
    while (~scanf("%d",&n)&&n)
    {
        memset(dp,0,sizeof(dp));
        scanf("%lf %lf %lf %lf %lf",&a,&b,&c,&d,&e);
        dp[n][1]=0;
        //三种情况概率各不相同,需要分开讨论
        for (int i=n; i>0; i--)
        {
            for (int j=1; j<=i; j++)
            {
                 //第一中情况第n行只能往左走,概率为1
                if (i==n&&j!=1)
                {
                    //dp[i][j-1]里存放的为上一步的期望值,在只能往走左的情况下,往左走的概率为1,所以dp[i][j]=(dp[i][j-1]+1)*1
                    dp[i][j]=(dp[i][j-1]+1)*1;
                }
                //第二种情况,第一列能往左下走概率为a和往右下走概率为b;
                if (j==1&&i!=n)
                {
                    //dp[i+1][j],dp[i+1][j+1]里存放的为上一步的期望值
                    //在能往左下走概率为a和往右下走概率为b的情况下,
                    //所以dp[i][j]=a*(dp[i+1][j]+1)+b*(dp[i+1][j+1]+1);
                    dp[i][j]=a*(dp[i+1][j]+1)+b*(dp[i+1][j+1]+1);
                }
                //第三种情况,其他地方则可以往左下走概率为c,往右下走概率为d,往左走概率为e;
                if(i!=n&&j!=1)
                {
                    dp[i][j]=e*(dp[i][j-1]+1)+c*(dp[i+1][j]+1)+d*(dp[i+1][j+1]+1);
                }
            }
        }
        printf("%.2lf\n",dp[1][1]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39535750/article/details/79705472