UPC2225: The number of steps

来源:互联网 发布:js正则表达式 编辑:程序博客网 时间:2024/04/27 02:53


2225: The number of steps

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 120  Solved: 74
[Submit][Status][Web Board]

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

There are no more than 70 test cases. 
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

Source

2013年山东省第四届ACM大学生程序设计竞赛



#include <iostream>#include <vector>#include <cstring>#include <cstdio>using namespace std;double f[50][50];bool vis[50][50];int n;double a,b,c,d,e;double dp(int i,int j){    if(vis[i][j]) return f[i][j];//vis就是标记用的,所以f里存放的是期望值    vis[i][j]=true;//每个坐标只便利一遍    double &ans=f[i][j];//ans存放 的是最后一次求的期望这里定义的是引用//    ans=0;//所以此时的ans就是f[i][j],并初始化f[i][j]为0    //明显下面是三种情况,第一个是只有左边    //第二个是有座下右下的时候,第三个是三个方向都有的时候    //j就是分左右的变量,往左走就是j-1,左下就是i+1,j-1,右下就是i+1,j+1    if(j-1>=0&&i+1>n) ans=dp(i,j-1)+1;    //i存放的层数,开始是从第一层开始    //i+1就是往下一层走,那么j是什么意思呢?此情况及时到了最后一次层了,只能往左走    //    else if(j-1<0&&i+1<=n) ans=a*dp(i+1,j)+b*dp(i+1,j+1)+1;    else if(j-1>=0&&i+1<=n) ans=e*dp(i,j-1)+c*dp(i+1,j)+d*dp(i+1,j+1)+1;    return ans;    //但是这里的算法还是不太明白    //我们平时求的饿期望是求出步数,再求每一类步数的概率,最后分别相乘再相加。    //这里的dp是先求离终点最近的每一小步的概率,同时乘步数,我列举了一下    //也就是分别把每一次的期望求出来了吧,}int main(){    while(scanf("%d",&n)!=EOF&&n)    {        scanf("%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e);        memset(vis,0,sizeof(vis));        f[n][0]=0;        printf("%.2lf\n",dp(1,0));    }    return 0;}//手动dp了一遍学长的这个题,还是不大懂


0 0