您的位置:首页 > 其它

13年山东省赛——The number of steps

2017-04-13 21:15 387 查看


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

这里有个链接可以帮助理解:http://kicd.blog.163.com/blog/static/126961911200910168335852/

dp[i][j]从i,j位置到终点的期望步数,所以每算一次要累加,因为是状态转移,注意+1表示多走一步(因为这个怀疑了半天。。。)

#include <iostream>

#include <cstdio>

#include <cstring>

#include <queue>

#include <cmath>

#include <algorithm>

#include <vector>

#include <map>

#include <string>

#include <stack>

using namespace std;

typedef long long ll;

#define PI 3.1415926535897932

#define E 2.718281828459045

#define INF 0x3f3f3f3f

#define mod 100000007

const int M=1005;

int n,m;

int cnt;

int sx,sy,sz;

int mp[1000][1000];

int pa[M*10],rankk[M];

int head[M*6],vis[M*100];

int dis[M*100];

ll prime[M*1000];

bool isprime[M*1000];

int lowcost[M],closet[M];

char st1[5050],st2[5050];

int len[M*6];

typedef pair<int ,int> ac;

//vector<int> g[M*10];

double dp[M][M];

int has[10500];

int month[13]= {0,31,59,90,120,151,181,212,243,273,304,334,0};

int dir[8][2]= {{0,1},{0,-1},{-1,0},{1,0},{1,1},{1,-1},{-1,1},{-1,-1}};

void getpri()

{

    ll i;

    int j;

    cnt=0;

    memset(isprime,false,sizeof(isprime));

    for(i=2; i<1000000LL; i++)

    {

        if(!isprime[i])prime[cnt++]=i;

        for(j=0; j<cnt&&prime[j]*i<1000000LL; j++)

        {

            isprime[i*prime[j]]=1;

            if(i%prime[j]==0)break;

        }

    }

}

struct node

{

    int v,w;

    node(int vv,int ww)

    {

        v=vv;

        w=ww;

    }

};

vector<int> g[M*100];

string str[1000];

int main()

{

    int i,j,k,t;

    double a,b,c,d,e;

    while(~scanf("%d",&n)&&n)

    {

        scanf("%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e);

        memset(dp,0,sizeof(dp));

        dp

=0;

            for(i=n-1;i>=0;i--)dp
[i]+=1.0*(dp
[i+1]+1);//当前位置右边走到终点只能往左走,所以当前的期望=右边+1

            for(i=n-1;i>0;i--){

                dp[i][i]+=b*(dp[i+1][i]+1)+a*(dp[i+1][i+1]+1);

                for(j=i-1;j>0;j--){

                    dp[i][j]+=d*(dp[i+1][j]+1)+c*(dp[i+1][j+1]+1)+e*(dp[i][j+1]+1);

                }

            }

        

        /*dp
[1]=0;

            for(i=2;i<=n;i++)dp
[i]+=1.0*(dp
[i-1]+1);//当前位置走到终点只能往左走,所以左边一步的期望+1

            for(i=n-1;i>0;i--){

                dp[i][1]+=a*(dp[i+1][1]+1)+b*(dp[i+1][2]+1);

                for(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);

                }

            }*/

        printf("%.2lf\n",dp[1][1]);

    }

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: