zoukankan      html  css  js  c++  java
  • [2013山东ACM]省赛 The number of steps (可能DP,数学期望)

    The number of steps

    Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

    题目描写叙述

        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?


    输入

    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.

    输出

    Please calculate the expected number of steps required to reach the KEY room, there are 2 digits after the decimal point.

    演示样例输入

    3
    0.3 0.7
    0.1 0.3 0.6
    0 

    演示样例输出

    3.41

    提示

     

    来源

    2013年山东省第四届ACM大学生程序设计竞赛

    解题思路:

    对于数学期望的定义是这种。数学期望
    E(X) = X1*p(X1) + X2*p(X2) + …… + Xn*p(Xn)

    比方打靶打中8环的概率为0.3  ,打中7环的概率为0.7,那么打中环数的期望就是 8*0.3 + 7*0.7

    本题中对于左边没有屋子的仅仅能向左下和右下走,概率为a,b,对于左边。左下,右下都有屋子的,三种方向都能够走,概率为 e,c,d,对于没有左下,没有右下仅仅能向左走的概率为1.问从顶部到左下角期望走的步数。

    就是求数学期望,但预先不知道离散的走的步数。所以须要用到期望的离散性质。

    本题中我们用dp[i][j] 表示当前位置距离目的地还须要走的期望步数。那么目的地如果为dp[n][1] (依据建的坐标不一样,位置也不一样),那么dp[n][1]的值为0,由于不须要再走了,那么我们所求的就是dp[1][1] 開始的地方。所以解题的过程。就是一个逆推的过程。

    以下,我们用两种视角(来解决本题)

    代码:

    首先以我们的视角:

    #include <iostream>
    #include <string.h>
    #include <iomanip>
    using namespace std;
    double dp[100][100];
    int n;
    double a,b,c,d,e;
    
    int main()
    {
        while(cin>>n&&n)
        {
            cin>>a>>b>>c>>d>>e;
            memset(dp,0,sizeof(dp));
            dp[n][1]=0;
            for(int i=2;i<=n;i++)
                dp[n][i]+=1*(dp[n][i-1]+1);//处理最后一行
            for(int i=n-1;i>=1;i--)//从倒数第二行開始处理
            {
                dp[i][1]+=a*(dp[i+1][1]+1)+b*(dp[i+1][2]+1);//处理每一行的第一列
                for(int j=2;j<=n;j++)
                    dp[i][j]+=c*(dp[i+1][j]+1)+d*(dp[i+1][j+1]+1)+e*(dp[i][j-1]+1);//处理每一行的除了第一列以外的其他列
            }
            cout<<setiosflags(ios::fixed)<<setprecision(2)<<dp[1][1]<<endl;
        }
        return 0;
    }

    以故事中主人公的视角:

    #include <iostream>
    #include <string.h>
    #include <iomanip>
    using namespace std;
    double dp[100][100];
    int n;
    double a,b,c,d,e;
    
    int main()
    {
        while(cin>>n&&n)
        {
            cin>>a>>b>>c>>d>>e;
            memset(dp,0,sizeof(dp));
            dp[n][n]=0;//目的地
            for(int i=n-1;i>=1;i--)
                dp[n][i]+=1*(dp[n][i+1]+1);
            for(int i=n-1;i>=1;i--)//行数
            {
                dp[i][i]+=a*(dp[i+1][i+1]+1)+b*(dp[i+1][i]+1);
                for(int j=i-1;j>=1;j--)
                    dp[i][j]+=c*(dp[i+1][j+1]+1)+d*(dp[i+1][j]+1)+e*(dp[i][j+1]+1);
            }
            cout<<setiosflags(ios::fixed)<<setprecision(2)<<dp[1][1]<<endl;
        }
        return 0;
    }



     

    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    Duang!异地多活!_InfoQ_传送门
    异地多活设计辣么难?其实是你想多了!-博客-云栖社区-阿里云
    途牛谭俊青:多数据中心状态同步&两地三中心的理论
    魅族多机房部署方案
    荔枝FM架构师刘耀华:异地多活IDC机房架构
    zz
    “异地多活”多机房部署经验谈_运维管理_数据中心_中国IDC圈
    天猫双11晚会和狂欢城的互动技术方案
    案例 | 魅族云端同步的架构实践和协议细节
    如何应对大促?看京东核心中间件团队的高可用实践指南
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4840703.html
Copyright © 2011-2022 走看看