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 *P22+ E4 * 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)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • fedora 12 QQ 的安装使用过程「建议收藏」

    fedora 12 QQ 的安装使用过程「建议收藏」一 下载地址:http://im.qq.com/qq/linux/download.shtml  由于装的是fedora  12于是下linuxqq-v1.0.2-beta1.i386.rmp直接双击安装就可以了。安装完后,应用程序出现在Internet下的腾讯QQ。二 成功登录后,不小心按了最小化后,郁闷的发现找不到qq图标。其实解决很简单,在面板上右击,选择添加到面板

    2022年9月2日
  • 上市ASCII 表省内发现!

    上市ASCII 表省内发现!

    2022年1月17日
  • Linux CentOS完全卸载PHP

    Linux CentOS完全卸载PHP

    2021年10月26日
  • DDD(领域驱动设计)

    基本概念:  领域驱动设计(简称ddd)概念来源于2004年著名建模专家ericevans发表的他最具影响力的书籍:《domain-drivendesign–tacklingcomplexityintheheartofsoftware》(中文译名:领域驱动设计—软件核心复杂性应对之道)一书。,书中提出了“领域驱动设计(简称ddd)”的概念。领域驱动设…

    2022年4月4日
  • JS设置定时器_js设置定时器

    JS设置定时器_js设置定时器JS定时器的一些特性和如何避免重复设置定时器概述和总结每个JS定时器产生时会被系统分配一个id,这个id是正整数,而且一个页面里面的定时器id不重复,我们能用一个变量接收这个id,但是如果重复执行一条接收创建语句,那么你只能接收到最新创建的定时器的id,之前创建的定时器的id会被覆盖,但是定时器数量在增加,这就会导致界面一些功能错乱,解决方法就是在重复按开始按钮时,如果已经有了一个定时器那么就不执行语句,我列出了错误代码和三种解决方法,可以解决定时器重复创建问题。ps:定时器id的配发是递增的,从1开

    2022年10月30日
  • HTML+CSS美食静态网页设计

    HTML+CSS美食静态网页设计爱尚美食网页用AdobeDreamweaverCS6制作代码如下:HTML代码:<!DOCTYPEhtml><html><head><metacharset=utf-8″/><title>爱尚美食</title><!–链接图片及css–><linkrel=”…

    2022年9月23日

发表回复

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

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