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


相关推荐

  • easyOCR_功能测试包括

    easyOCR_功能测试包括EasyOCR是一个用python编写的OCR三方库。git地址为:https://github.com/JaidedAI/EasyOCR。由于笔者从事的是java开发,对python并不熟悉,所以实际上是从python开发环境安装开始的。类似于jdk,python开发也依赖于python环境,而因为python各版本之间差异很大,很多时候不同组件依赖的是不同的python版本,甚至小版本之间也存在兼容性问题,所以网上推荐使用的是Anaconda环境管理软件。Anaconda可以隔离出多个pytho

    2025年5月23日
    4
  • python自动炒股软件下载_python自动股票交易软件

    python自动炒股软件下载_python自动股票交易软件获取数据是数据分析中必不可少的一部分,而网络爬虫是是获取数据的一个重要渠道之一。鉴于此,我拾起了Python这把利器,开启了网络爬虫之路。本篇使用的版本为python3.5,意在抓取证券之星上当天所有A股数据。程序主要分为三个部分:网页源码的获取、所需内容的提取、所得结果的整理。一、网页源码的获取很多人喜欢用python爬虫的原因之一就是它容易上手。只需以下几行代码既可抓取大部分网页的源码。imp…

    2022年6月19日
    42
  • get请求与post提交区别的简易理解

    get请求与post提交区别的简易理解

    2021年7月16日
    65
  • jar find小技巧

    jar find小技巧

    2021年5月12日
    143
  • idea激活码在哪输入2022【2022.01最新】2022.02.03

    (idea激活码在哪输入2022)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlCJ…

    2022年3月31日
    229
  • java中集合转数组中_JAVA中集合转数组遍历[通俗易懂]

    java中集合转数组中_JAVA中集合转数组遍历[通俗易懂]JAVA中集合的遍历的一种方法时集合转数组遍历,也是就调用Collection中的toArray().代码:publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubCollectionc=newArrayList();c.add(newStudent(“kj”,12));c.add(newStude…

    2022年6月15日
    29

发表回复

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

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