The number of steps

nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; padding-right:0px; color:rgb(83,113,197); text-decoration:none; padding-top:0px">

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大学生程序设计竞赛

解题思路:

对于数学期望的定义是这种。数学期望
E(X) = X1*p(X1) + X2*p(X2) + …… + Xn*p(Xn)

比方打靶打中8环的概率为0.3  ,打中7环的概率为0.7,那么打中环数的期望就是 8*0.3 + 7*0.7

本题中对于左边没有屋子的仅仅能向左下和右下走,概率为a,b,对于左边。左下,右下都有屋子的,三种方向都能够走,概率为 e,c,d,对于没有左下,没有右下仅仅能向左走的概率为1.问从顶部到左下角期望走的步数。

就是求数学期望,但预先不知道离散的走的步数。所以须要用到期望的离散性质。

本题中我们用dp[i][j] 表示当前位置距离目的地还须要走的期望步数。那么目的地如果为dp[n][1] (依据建的坐标不一样,位置也不一样),那么dp[n][1]的值为0,由于不须要再走了,那么我们所求的就是dp[1][1] 開始的地方。所以解题的过程。就是一个逆推的过程。

以下,我们用两种视角(来解决本题)

代码:

首先以我们的视角:

#include <iostream>
#include <string.h>
#include <iomanip>
using namespace std;
double dp[100][100];
int n;
double a,b,c,d,e; int main()
{
while(cin>>n&&n)
{
cin>>a>>b>>c>>d>>e;
memset(dp,0,sizeof(dp));
dp[n][1]=0;
for(int i=2;i<=n;i++)
dp[n][i]+=1*(dp[n][i-1]+1);//处理最后一行
for(int i=n-1;i>=1;i--)//从倒数第二行開始处理
{
dp[i][1]+=a*(dp[i+1][1]+1)+b*(dp[i+1][2]+1);//处理每一行的第一列
for(int j=2;j<=n;j++)
dp[i][j]+=c*(dp[i+1][j]+1)+d*(dp[i+1][j+1]+1)+e*(dp[i][j-1]+1);//处理每一行的除了第一列以外的其他列
}
cout<<setiosflags(ios::fixed)<<setprecision(2)<<dp[1][1]<<endl;
}
return 0;
}

以故事中主人公的视角:

#include <iostream>
#include <string.h>
#include <iomanip>
using namespace std;
double dp[100][100];
int n;
double a,b,c,d,e; int main()
{
while(cin>>n&&n)
{
cin>>a>>b>>c>>d>>e;
memset(dp,0,sizeof(dp));
dp[n][n]=0;//目的地
for(int i=n-1;i>=1;i--)
dp[n][i]+=1*(dp[n][i+1]+1);
for(int i=n-1;i>=1;i--)//行数
{
dp[i][i]+=a*(dp[i+1][i+1]+1)+b*(dp[i+1][i]+1);
for(int j=i-1;j>=1;j--)
dp[i][j]+=c*(dp[i+1][j+1]+1)+d*(dp[i+1][j]+1)+e*(dp[i][j+1]+1);
}
cout<<setiosflags(ios::fixed)<<setprecision(2)<<dp[1][1]<<endl;
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

[2013山东ACM]省赛 The number of steps (可能DP,数学期望)的更多相关文章

  1. 13年山东省赛 The number of steps(概率dp水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud The number of steps Time Limit: 1 Sec  Me ...

  2. The number of steps(概率dp)

    Description Mary stands in a strange maze, the maze looks like a triangle(the first layer have one r ...

  3. [2012山东ACM省赛] Pick apples (贪心,完全背包,枚举)

    Pick apples Time Limit: 1000MS Memory limit: 165536K 题目描述 Once ago, there is a mystery yard which on ...

  4. [2012山东ACM省赛] Pick apples (贪心,全然背包,枚举)

    Pick apples Time Limit: 1000MS Memory limit: 165536K 题目描写叙述 Once ago, there is a mystery yard which ...

  5. [2011山东ACM省赛] Sequence (动态规划)

    Sequence Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Given an integer number sequence ...

  6. 2013年ACM湖南省赛总结

    今年的比赛最大的变化就是改用OJ判题了,相比于PC^2确实省事了不少,至少可以直接复制样例了.题目方面依旧是刘汝佳命题,这点还是相当好的,至少给人以足够的安全感. 开始比赛之后安叔瞬间就把前半部分题目 ...

  7. [2011山东ACM省赛] Identifiers(模拟)

    Identifiers Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述  Identifier is an important ...

  8. [2011山东ACM省赛] Mathman Bank(模拟题)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/sr19930829/article/details/24187925 Mathman Bank ni ...

  9. 山东ACM省赛历届入口

    山东省第一届ACM大学生程序设计竞赛 山东省第二届ACM大学生程序设计竞赛 山东省第三届ACM大学生程序设计竞赛 山东省第四届ACM大学生程序设计竞赛 山东省第五届ACM大学生程序设计竞赛 山东省第六 ...

随机推荐

  1. BZOJ 3747 POI2015 Kinoman 段树

    标题效果:有m点,每个点都有一个权值.现在我们有这个m为点的长度n该序列,寻求区间,它仅出现一次在正确的点区间内值和最大 想了很久,甚至神标题,奔说是水的问题--我醉了 枚举左点 对于每个请求留点右键 ...

  2. 使用vbs脚本添加域网络共享驱动器

    MapNetworkDrive Method Adds a shared network drive to your computer system. object.MapNetworkDrive(s ...

  3. 手提wifi双卡配置+window7同时多用户远程+有些公司限制网络环境方案

    该公司只提供几台机器,同时限制并连接到内部办公网络的机, 我们更多的临时工作人员,项目紧张,而另一种是太麻烦了申请, 当被问及其他网络管理,说没有变通方法. 在我的尝试,最后,找到一个解决方案; 解决 ...

  4. [LeetCode299]Bulls and Cows

    题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...

  5. ios 多线程开发(二)线程管理

    线程管理 iOS和OS X中每一个进程(或程序)由一个或多个线程组成.程序由一个运行main方法的线程开始,中间可以产生其他线程来执行一些指定的功能. 当程序产生一个新线程后,这个线程在程序进程空间内 ...

  6. Extjs Web Desktop申请书

    今天我Web Desktop应用基本完成.多语言支持.现有asp,php,jsp版本号. 废话拍了几张照片让大家有一个直观的了解: watermark/2/text/aHR0cDovL2Jsb2cuY ...

  7. XML数据读取方式性能比较(一)

    原文:XML数据读取方式性能比较(一) 几个月来,疑被SOA,一直在和XML操作打交道,SQL差不多又忘光了.现在已经知道,至少有四种常用人XML数据操作方式(好像Java差不多),不过还没有实际比较 ...

  8. ASP.NET自定义控件组件开发 第四章 组合控件开发CompositeControl 后篇 --事件冒泡

    原文:ASP.NET自定义控件组件开发 第四章 组合控件开发CompositeControl 后篇 --事件冒泡 CompositeControl  后篇 --事件冒泡 系列文章链接: ASP.NET ...

  9. 允许debian wheezy支持IOS7+的iphone.

    IOS更新, 连接到数据线,不能使用 我想复制iphone照片只能用于内itunes对?  于linux这里面其实很容易处理. 在这里,我们使用了一个相对较新的组件libimobiledevice 为 ...

  10. 自己动手写一个编译器Tiny语言解析器实现

    然后,上一篇文章简介Tiny词法分析,实现语言.本文将介绍Tiny的语法分析器的实现. 1 Tiny语言的语法 下图是Tiny在BNF中的文法. 文法的定义能够看出.INNY语言有以下特点: 1 程序 ...