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


相关推荐

  • Qt css颜色对照表

    Qt css颜色对照表css颜色代码对照FFFFFF#DDDDDD#AAAAAAFFFFFF#DDDDDD#AAAAAA#888888#666666#444444#000000#FFB7DD#FF88C2#FF44AA#FF0088#C10066#A20055#8C0044#FFCCCC#FF8888#FF3333#FF0000#CC0000#AA0000#880000#FFC8B4#FFA488#FF7744

    2022年5月17日
    84
  • sshd服务设定root登陆配置项PermitRootLogin的解析「建议收藏」

    sshd服务设定root登陆配置项PermitRootLogin的解析「建议收藏」首先看一下sshd_config中关于PermitRootLogin的配置信息:#grepPermitRootLogin/etc/ssh/sshd_configPermitRootLoginyes#thesettingof”PermitRootLoginwithout-password”.那么PermitRootLoginwithout-password又是什么意义呢?PermitRootLogin配置项都有哪些配置参数?常见:yes,no比较陌生:withou

    2022年6月11日
    164
  • RPG Maker MV攻略_游戏解包工具

    RPG Maker MV攻略_游戏解包工具该文章最新版本请前往:https://www.crowsong.xyz/127.html前言使用Petschko'sRPG-Maker-MVFile-Decrypter进行解包使用P

    2022年8月6日
    167
  • 计算机中完成全选的快捷键,怎么全选-很实用!word中全选的快捷键介绍及使用方法…[通俗易懂]

    计算机中完成全选的快捷键,怎么全选-很实用!word中全选的快捷键介绍及使用方法…[通俗易懂]全选快捷键可以提高我们在操作word时工作效率,在操作Word2003中怎么对文档中的文字进行全选呢?下面为大家提供几种全选的方法,绝对好用。Word怎样全选?方法一、使用Word全选快捷键“Ctrl+A”进行全选(也适用于电子表格);方法二、展开菜单栏中的“编辑”,然后选择“全选”按钮来全选;方法三、利用鼠标全选,鼠标左键按住不放然后拖动到最后也可以全选;方法四、鼠标单击开始部分,然后在最末尾部…

    2022年5月9日
    133
  • ZXV10 H608B V1.1.04T02_JS激活成功教程

    ZXV10 H608B V1.1.04T02_JS激活成功教程综合了网上各种说法,得出如下方案:一、如果你的路由器还能够用用户名telecomadmin密码nE7jA%5m登陆,那就拔掉电话线直接跳到步骤八二、拔掉路由器的电话线,下载提供的包并解压。三、打开包中

    2022年7月1日
    21
  • Java 枚举 (enum) 使用方法

    Java 枚举 (enum) 使用方法什么是枚举 枚举类型是 Java5 中新增特性的一部分 它是一个特殊的 class 这个 class 相当于 finalstatic 修饰 不能被继承 所有的枚举都继承自 java lang Enum 类 由于 Java 不支持多继承 所以枚举对象不能再继承其他类 在没有枚举类型时定义常量常见的方式 Createdbyzej 5 7 Blog http blog csdn net javazejian 原文地址 请尊重原创 使用普通方式定义日期常量

    2025年8月11日
    0

发表回复

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

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