sdut2623–The number of steps(概率dp第一弹,求期望)

sdut2623–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大学生程序设计竞赛
概率dp的第一道题目,题目比較简单。
到着求解,最后一个点到最后的期望是0,其它的都由它连接的点的期望求出来。

sdut2623--The number of steps(概率dp第一弹,求期望)
假设i到j的概率是p
ij,
i到i的概率是p
ii
,期望是E,那么求1到4的期望是
1.   E4 = 0 。
2.   E3 =E3 *P33 E4 * P34 + 1 ;
3.   E2 = E2 *P22E4 * P24 + 1  ;
4.   E1 =E1 *P11 + E2 *P12 +E3 * P13 + 1  ;
记忆化搜索,最后推出要求的值
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
double dp[100][100] ;
double a , b , c , d , e ;
int i , j , n ;
int ff(int x,int y)
{
    if( x <= n && y >=(n+1)-x )
        return 1 ;
    return 0 ;
}
void f()
{

    return ;
}
int main()
{
    while(scanf("%d", &n) && n)
    {
        scanf("%lf %lf", &a, &b);
        scanf("%lf %lf %lf", &c, &d, &e);
        memset(dp,0,sizeof(dp));
        for(i = n ; i >= 1 ; i--)
        {
            for(j = (n+1)-i ; j <= n ; j++)
            {
                if(i == n && j == (n+1)-i) continue ;
                else if( i == n )
                    dp[i][j] = 1.0*( dp[i][j-1] ) + 1.0 ;
                else
                {
                    if( j == (n+1)-i )
                        dp[i][j] = a*dp[i+1][j-1] + b*dp[i+1][j] + 1.0 ;
                    else
                        dp[i][j] = c*dp[i+1][j-1] + d*dp[i+1][j] + e*dp[i][j-1] + 1.0 ;
                }
            }
        }
        printf("%.2lf\n", dp[1][n]);
    }
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115755.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • R6034错误解决办法_错误1962解决办法

    R6034错误解决办法_错误1962解决办法转载自:http://hi.baidu.com/%B3%E6%B5%C4%B4%AB%C8%CB/blog/item/1ee503e785263324b838206f.html提示没有找到MSVCR80D.dllR6034AnapplicationhasmadeanattempttoloadtheCruntimelibrarywithoutusinga

    2025年7月6日
    4
  • php工厂模式使用场景[通俗易懂]

    php工厂模式使用场景[通俗易懂]场景:使用工厂模式接入:阿里短信验证、腾讯短信验证、百度短信验证创建类文件BaseSMS.php–基础短信服务接口类AliSMS.php–阿里短信服务类BaiduSMS.php–百度短信服务类TencentSMS.php–腾讯短信服务类SmsBusiness.php–短信业务逻辑类具体代码BaseSMS.php–基础短信服务接口类interfaceBaseSMS{publicstaticfunctionsendCode($phone,$co

    2022年7月25日
    23
  • 可以用verilog描述而不能用VHDL_verilog多次调用同一模块

    可以用verilog描述而不能用VHDL_verilog多次调用同一模块今天在编译一个Verilog文件,其中嵌入了VHDL的模块,其VHDL模块如下:entityvhdl_moduleisgeneric(PARA1:boolean:=false;–boolean型PARA2:boolean:=false;–integral型);

    2025年12月7日
    5
  • Ubuntu 16.04 下安装VMware Tools(三行命令搞定,亲测好使)

    Ubuntu 16.04 下安装VMware Tools(三行命令搞定,亲测好使)Ubuntu16.04下安装VMwareTools(三行命令搞定,亲测好使):第一行命令:sudoapt-getupgrate第二行命令:sudoapt-getinstallopen-vm-tools-desktop-y第三行命令:sudoreboot如果觉得好使,请点赞;…

    2022年5月26日
    52
  • matlab 折线图 标记_matlab画折线图标记线

    matlab 折线图 标记_matlab画折线图标记线…’MarkerSize’,10)xlabel(‘x’);ylabel(‘y’);·用Matlab画图时,有时候需要对各种图标进行标注,例如,用“+”代表A的运动情况,“*”代表……画出来就成了折线图,请试验之(*);(,’:’,”)同时画两个函数若要改变颜色,在座标对后面加上相关字串即可:;((),”)若要同时改变颜色及图线……是用m…

    2022年4月30日
    42
  • Java语言中一个字符占几个字节?「建议收藏」

    Java语言中一个字符占几个字节?「建议收藏」要区分清楚内码(internalencoding)和外码(externalencoding)就好了。内码是程序内部使用的字符编码,特别是某种语言实现其char或String类型在内存里用的内部编码;外码是程序与外部交互时外部使用的字符编码。“外部”相对“内部”而言;不是char或String在内存里用的内部编码的地方都可以认为是“外部”。例如,外部可以是序列化之后的char或String…

    2022年6月26日
    28

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号