博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
The number of steps(概率dp)
阅读量:5051 次
发布时间:2019-06-12

本文共 3054 字,大约阅读时间需要 10 分钟。

Description

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?

Input

In each case , first Input a positive integer n(0<n<45), which means the layer of the maze, then Input five real number a, b, c, d, e. (0<=a,b,c,d,e<=1, a+b=1, c+d+e=1).

The input is terminated with 0. This test case is not to be processed.

 

 

Output

Please calculate the expected number of steps required to reach the KEY room, there are 2 digits after the decimal point.

Sample Input

30.3 0.70.1 0.3 0.60

Sample Output

3.41

Hint

题解:

数学期望E(X) = X1*p(X1) + X2*p(X2) + …… + Xn*p(Xn); 
比如打靶打中8环的概率为0.3 ,打中7环的概率为0.7,那么打中环数的期望就是 8*0.3 + 7*0.7; 
本题中我们用dp[i][j] 表示当前位置(i,j,表示房间的位置,最顶层的房间为(1,1),最低层最左边为(n,1))距离目的地还需要走的期望步数。那么目的地假设为dp[n][1] (根据建的坐标不一样,位置也不一样),那么dp[n][1]的值为0,因为已经到达目的地,不需要再走了。那么我们所求的就是dp[1][1] 开始的地方。所以解题的过程,就是一个逆推的过程。整个逆推过程完成,dp[1][1]内的值就是所求的期望步数。

代码:

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define is_lower(c) (c >= 'a' && c <= 'z')#define is_upper(c) (c >= 'A' && c <= 'Z')#define is_alpha(c) (is_lower(c) || is_upper(c))#define is_digit(c) (c >= '0' && c <= '9')#define min(a, b) ((a) < (b) ? (a) : (b))#define max(a, b) ((a) > (b) ? (a) : (b))#define PI acos(-1)#define IO \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0);#define For(i, a, b) for (int i = a; i <= b; i++)typedef long long ll;typedef unsigned long long ull;typedef pair
pii;typedef pair
pll;typedef vector
vi;const ll inf = 0x3f3f3f3f;const double EPS = 1e-10;const ll inf_ll = (ll)1e18;const ll maxn = 100005LL;const ll mod = 1000000007LL;const int N = 50 + 5;double ans[N][N];int main() { int n; while (cin >> n, n) { memset(ans, 0, sizeof(ans)); double a, b, c, d, e; cin >> a >> b >> c >> d >> e; For(i, 2, n) { ans[n][i] = ans[n][i - 1] + 1; } for (int i = n - 1; i >= 1; i--) { ans[i][1] = (ans[i + 1][1] + 1) * a + (ans[i + 1][2] + 1) * b; for (int j = 2; j <= i; j++) { ans[i][j] = (ans[i][j - 1] + 1) * e + (ans[i + 1][j] + 1) * c + (ans[i + 1][j + 1] + 1) * d; } } printf("%.2lf\n",ans[1][1]); }}

 

 

转载于:https://www.cnblogs.com/GHzz/p/8622697.html

你可能感兴趣的文章
delphi.指针.PChar
查看>>
Objective - C基础: 第四天 - 10.SEL类型的基本认识
查看>>
android调试debug快捷键
查看>>
【读书笔记】《HTTP权威指南》:Web Hosting
查看>>
Inoodb 存储引擎
查看>>
数据结构之查找算法总结笔记
查看>>
Android TextView加上阴影效果
查看>>
Requests库的基本使用
查看>>
C#:System.Array简单使用
查看>>
「Foundation」集合
查看>>
二叉树的遍历 - 数据结构和算法46
查看>>
类模板 - C++快速入门45
查看>>
RijndaelManaged 加密
查看>>
Android 音量调节
查看>>
windows上面链接使用linux上面的docker daemon
查看>>
Redis事务
查看>>
Web框架和Django基础
查看>>
python中的逻辑操作符
查看>>
HDU 1548 A strange lift (Dijkstra)
查看>>
每天一个小程序—0005题(批量处理图片大小)
查看>>