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


相关推荐

  • 软件测试(3) UFT12使用_GUITest

    软件测试(3) UFT12使用_GUITest环境:UFT12,Win10,VS2015&VS2017启动UFT12,为了启动方便修改快捷方式。设置插件,WPF相关的都设置起来。新建项目点击录制按钮(F6),设置启动程序。确定,开始录制。程序启动,显示登录界面。操作:切换IP,点击按钮,登录。点击工具栏按钮,停止录制。点击运行按钮(F5)。

    2022年5月27日
    59
  • python将一维数组导入到excel表格,并使用Origin绘图

    python将一维数组导入到excel表格,并使用Origin绘图python将一维数组导入到excel表格,并使用excel绘图

    2022年5月30日
    36
  • Python画图显示中文

    Python画图显示中文matplotlib作图时默认设置下为英文,无法显示中文,只需要添加下面两行代码即可plt.rcParams[‘font.sans-serif’]=[‘SimHei’]plt.rcParams[‘axes.unicode_minus’]=FalseExampleimportmatplotlib.pyplotaspltfromnumpy.randomimportmul…

    2022年6月1日
    37
  • 5g切片技术详解_5G切片SLA

    5g切片技术详解_5G切片SLA网络切片是5G网络的关键特征,可以在共享的基础设施上构建专用的逻辑网络。5G网络切片代表了一种网络架构,它允许独立和虚拟化的逻辑网络复用同一个物理网络基础设施。每个切片都是一个独立的端到端网络,以满足特定应用程序的各种需求。其实,切片在5G中并不是一个新概念,在4G中也存在(例如,APN、MORAN和GWCN),但其能力有限,因为它们不像在5G中那样支持完整的端到端解决方案。简单起见,假设运营商网络是一块大蛋糕,我们将其分成几片。每片包含整个网络的一部分,包括无线网络、传输网络和核心网络。此外,。。…

    2022年10月3日
    0
  • 持续集成之企业微信通知:3:推送消息示例(text、markdown、news)

    持续集成之企业微信通知:3:推送消息示例(text、markdown、news)在前面一篇文章中了解到了目前企业微信群机器人推送消息的4种格式,这篇文章以实际的使用示例来演示其中三种的使用

    2022年6月6日
    141
  • 装饰器 (Decorator)

    装饰器 (Decorator)装饰器 Decorator 是 个函数 用来修改类的行为 装饰器对类的行为的改变 是编译时发生的 而不是在运行时 作用 是 种动态地往 个类中添加新的行为的设计模式 它可以在类运行时 扩展 个类的功能 并且去修改类本身的属性和方法 使其可以在不同类之间更灵活的共用 些属性和方法 装饰器本庚就是编译时执行的函数 decoratorcla 等同于 classA A decorator A A 装饰类 testable 就是 个

    2025年7月9日
    0

发表回复

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

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