第四届 山东省ACM The number of steps (概率dp 待整理)

The number of steps

Time Limit: 1000MS Memory Limit: 65536KB

Problem 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
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.

Example Input

3
0.3 0.7
0.1 0.3 0.6
0 

Example Output

3.41

Hint

 

Author

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



一个金子塔型的迷宫,一个人在金子塔的定点,现在要走到金子塔的左下角,给出了这个人走左边的们,左下角的们,右下角的门,对应的概率,现在问这个人走到金子塔左下角步数的期望。

题解:

直接搞,E[i][j],表示位置i,j步数的期望,然后倒推,最终答案E[1][n]。很简单的一道概率dp。


#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
#define B(x) (1<<(x))
using namespace std;
typedef long long ll;
void cmax(int& a,int b){ if(b>a)a=b; }
void cmin(int& a,int b){ if(b<a)a=b; }
void cmax(ll& a,ll b){ if(b>a)a=b; }
void cmin(ll& a,ll b){ if(b<a)a=b; }
void add(int& a,int b,int mod){ a=(a+b)%mod; }
void add(ll& a,ll b,ll mod){ a=(a+b)%mod; }
const int oo=0x3f3f3f3f;
const int MOD=2012;
const int maxn=50;
double E[maxn][maxn];

int main(){
    //freopen("E:\read.txt","r",stdin);
    int n;
    double a,b,c,d,e;
    while(scanf("%d",&n)!=EOF){
        if(n==0)break;
        scanf("%lf %lf %lf %lf %lf",&a,&b,&c,&d,&e);
        for(int i=0;i<=n;i++)
            for(int j=0;j<=n;j++)
                E[i][j]=0.0;
        for(int j=2;j<=n;j++)
            E[n][j]=E[n][j-1]+1;
        for(int i=n-1;i>=1;i--){
            for(int j=n-i+1;j<=n;j++){
                if(j==n-i+1){
                    E[i][j]=(E[i+1][j-1]+1)*a+(E[i+1][j]+1)*b;
                }else{
                    E[i][j]=(E[i+1][j-1]+1)*c+(E[i+1][j]+1)*d+(E[i][j-1]+1)*e;
                }
            }
        }
        printf("%.2lf
",E[1][n]);
    }
    return 0;
}






原文地址:https://www.cnblogs.com/zswbky/p/6717889.html