SDUT 2623 The number of steps (概率)

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大学生程序设计竞赛
 
 
这里有个链接可以帮助理解: http://kicd.blog.163.com/blog/static/126961911200910168335852/
 
第一行有一个位置,第二行两个,第三行三个......第n行n个。此时你在最左上角的位置,如果你左面没有位置,只能往左下和右下走,概率(a,b)。否则可以往左,左下和右下三个方向走,,概率(c,d,e)。让你求到达最左下角的期望。
 
而我们可以逆向推导,即从最左下角走向最顶端,,自己推推。。。。。。。。
 
#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

double dp[110][110];
double a,b,c,d,e;

int main(){

    //freopen("input.txt","r",stdin);

    int n;
    while(~scanf("%d",&n) && n){
        scanf("%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e);
        memset(dp,0,sizeof(dp));
        dp[n][n]=0;
        for(int j=n-1;j>=1;j--)     //初始化最底下一行(即第n行)
            dp[n][j]+=1*(dp[n][j+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);  //初始化n-1~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); //初始化n-1~1行的除了最左边的位置的期望值
        }
        printf("%.2lf\n",dp[1][1]);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/jackge/p/3147774.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值