• hdu 4405 Aeroplane chess (期望DP)


    Aeroplane chess

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 6257    Accepted Submission(s): 3891


    Problem Description
    Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to N. Hzz starts at grid 0. For each step he throws a dice(a dice have six faces with equal probability to face up and the numbers on the faces are 1,2,3,4,5,6). When Hzz is at grid i and the dice number is x, he will moves to grid i+x. Hzz finishes the game when i+x is equal to or greater than N.

    There are also M flight lines on the chess map. The i-th flight line can help Hzz fly from grid Xi to Yi (0<Xi<Yi<=N) without throwing the dice. If there is another flight line from Yi, Hzz can take the flight line continuously. It is granted that there is no two or more flight lines start from the same grid.

    Please help Hzz calculate the expected dice throwing times to finish the game.
     
    Input
    There are multiple test cases. 
    Each test case contains several lines.
    The first line contains two integers N(1≤N≤100000) and M(0≤M≤1000).
    Then M lines follow, each line contains two integers Xi,Yi(1≤Xi<Yi≤N).  
    The input end with N=0, M=0. 
     
    Output
    For each test case in the input, you should output a line indicating the expected dice throwing times. Output should be rounded to 4 digits after decimal point.
     
    Sample Input
    2 0 8 3 2 4 4 5 7 8 0 0
     
    Sample Output
    1.1667 2.3441
     
    题目大意:
    数轴上有N+1个点(编号0~N),一个人玩游戏,从0出发,当到达N或大于N的点则游戏结束。每次行动掷骰子一次,骰子编号1-6,掷到多少就向前走几步,这个数轴上还有些特殊点,这些点类似飞行棋中的飞行点,只要到达这些点就可以直接飞到给定点。求总共投掷骰子次数的期望。
     
    用dp[i]表示在i位置时,距离游戏结束还要投掷次数的期望。显然dp[n]为0,需要求的是dp[0]。对于直接飞过去的点。例如用数组vis[]来表示,vis[a]=b,表示当到达a点时可以直接飞到b点,那么显然dp[vis[a]]=dp[a]。倒着推,dp[i](假设该点不属于可飞行的点)的下面一个状态有6种可能(即对应6种可能的骰子数),每种都是1/6的概率。所以for(int x=1;x<=6;x++)dp[i]+=dp[i+x]/6.0;dp[i]+=1;注意最后加玩每种可能性的期望后要+1,因为这6种可能性加起来只是下一个状态的期望,当前状态是他们的前一个状态,所以期望(直接理解为投掷骰子的次数)要+1。
     
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    
    using namespace std;
    
    const int maxn=100000;
    
    int to[maxn+10];
    double dp[maxn+10];
    
    int main()
    {
        int n,m;
        while(scanf("%d%d",&n,&m),n||m)
        {
            memset(to,-1,sizeof(to));
            for(int i=1,x,y; i<=m; i++)
            {
                scanf("%d%d",&x,&y);
                to[x]=y;
            }
            for(int i=0; i<=n+5; i++)
                dp[i]=0;
            for(int i=n-1; i>=0; i--)
            {
                if(to[i]!=-1)
                    dp[i]=dp[to[i]];
                else
                {
                    for(int j=1; j<=6; j++)
                    {
                        dp[i]+=(dp[i+j]+1)/6;
                    }
                }
            }
            printf("%.4f
    ",dp[0]);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    20150607-0608
    20150423
    感触
    NSString里面的数字
    Git使用相关
    状态栏的设置
    UI相关问题
    分栏(标签)控制器
    导航控制器
    NSAttributedString 属性
  • 原文地址:https://www.cnblogs.com/acboyty/p/9818534.html
Copyright © 2020-2023  润新知