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


相关推荐

  • Python爬虫从入门到精通——解析库pyquery的使用「建议收藏」

    Python爬虫从入门到精通——解析库pyquery的使用「建议收藏」分类目录:《Python爬虫从入门到精通》总目录在《解析库BeautifulSoup的使用》中,我们介绍了BeautifulSoup的用法,它是一个非常强大的网页解析库,但如果你对Web有所涉及,如果你比较喜欢用CSS选择器,如果你对jQuery有所了解,那么这里有一个更适合你的解析库——pyquery。pyquery初始化像BeautifulSoup一样,初始化pyquery的时候,…

    2022年6月9日
    39
  • Java程序员,你一定需要了解的六款大数据采集平台

    Java程序员,你一定需要了解的六款大数据采集平台随着大数据越来越被重视,数据采集的挑战变的尤为突出。今天为大家介绍几款数据采集平台: ApacheFlume Fluentd Logstash Chukwa Scribe SplunkForwarder 大数据平台与数据采集任何完整的大数据平台,一般包括以下的几个过程: 数据采集 数据存储 数据处理 …

    2022年6月6日
    127
  • java oracle 连接池_oracle数据库连接池配置

    java oracle 连接池_oracle数据库连接池配置频繁的创建和销毁数据库连接即消耗系统资源又使得程序效率低下,在这种情况下,出现了使用数据库连接池的方法,类似于线程池,初期创建一定数量的连接供应用程序使用,当使用完成后将其归还给连接池而不是销毁,这样有效的提高了资源利用率,下面分享一种简单的创建连接池的方法:1.首先,我们新建一个maven工程,并且导入ojdbc,dbcp,junit三个包待用2.然后,我…

    2025年11月29日
    7
  • 获取 Windows Phone 手机系统信息

    wpf:1161718192021222324252627282930…

    2021年12月20日
    49
  • 机器人手眼标定Ax=xB(eye to hand和eye in hand)及平面九点法标定[通俗易懂]

    机器人手眼标定Ax=xB(eye to hand和eye in hand)及平面九点法标定[通俗易懂]一、背景Calibration是机器人开发者永远的痛。虽然说方法说起来几十年前就有,但每一个要用摄像头的人都还是要经过一番痛苦的踩坑,没有轻轻松松拿来就效果好的包。机器人视觉应用中,手眼标定是一个非常基础且关键的问题。简单来说手眼标定的目的就是获取机器人坐标系和相机坐标系的关系,最后将视觉识别的结果转移到机器人坐标系下。手眼标定行业内分为两种形式,根据相机固定的地方不同,如果相机和机器…

    2022年4月27日
    100
  • eclispe中创建maven项目使用spring报java.lang.ClassNotFoundException: org.springframework.web.filter.Charact「建议收藏」

    eclispe中创建maven项目使用spring报java.lang.ClassNotFoundException: org.springframework.web.filter.Charact「建议收藏」报错如下:信息: Starting Servlet Engine: Apache Tomcat/7.0.57九月 24, 2018 6:44:04 下午 org.apache.catalina.util.SessionIdGenerator createSecureRandom信息: Creation of SecureRandom instance for session ID gen…

    2022年6月13日
    43

发表回复

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

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