• Aeroplane chess HDU 4405 概率期望DP


    Aeroplane chess

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


    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
     
    Source
     
    Recommend
    zhoujiaqi2010
     
    /*
     * Author:  
     * Created Time:  2013/10/17 18:16:04
     * File Name: A.cpp
     * solve: A.cpp
     */
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cmath>
    #include<algorithm>
    #include<string>
    #include<map>
    #include<stack>
    #include<set>
    #include<iostream>
    #include<vector>
    #include<queue>
    //ios_base::sync_with_stdio(false);
    //#pragma comment(linker, "/STACK:1024000000,1024000000")
    
    using namespace std;
    #define sz(v) ((int)(v).size())
    #define rep(i, a, b) for (int i = (a); i < (b); ++i)
    #define repf(i, a, b) for (int i = (a); i <= (b); ++i)
    #define repd(i, a, b) for (int i = (a); i >= (b); --i)
    #define clr(x) memset(x,0,sizeof(x))
    #define clrs( x , y ) memset(x,y,sizeof(x))
    #define out(x) printf(#x" %d
    ", x)
    #define sqr(x) ((x) * (x))
    typedef long long LL;
    
    const int INF = 1000000000;
    const double eps = 1e-8;
    const int maxn = 300000;
    
    int sgn(const double &x) {  return (x > eps) - (x < -eps); }
    
    int to[maxn];
    double dp[maxn];
    int main() 
    {
        //freopen("in.txt","r",stdin);
        int n,m;
        while(scanf("%d%d",&n,&m) == 2)
        {
            if(n + m == 0)
                break;
            clr(to);
            repf(i,1,m)
            {
                int a,b;
                scanf("%d%d",&a,&b);
                to[a] = b;
            }
            
            dp[n] = 0;
            clr(dp);
            repd(i,n-1,0)
            {
                if(to[i])
                    dp[i] = dp[to[i]];
                else
                {
                    repf(j,1,6)
                    {
                        if(i+j<=n)
                        dp[i] += dp[i+j]/6.0;
                    }
                    dp[i] += 1;
                }
                //dp[i] += 1;
            }
            printf("%.4lf
    ",dp[0]);
        }
        return 0;
    }
  • 相关阅读:
    python---基础知识回顾(九)图形用户界面-------Tkinter
    python---基础知识回顾(八)数据库基础操作(sqlite和mysql)
    python---重点(设计模式)
    python---基础知识回顾(七)迭代器和生成器
    python---权限管理和菜单生成
    python---基础知识回顾(六)网络编程
    python---基础知识回顾(五)(python2.7和python3.5中的编码)
    CustomProgressBar
    android-square-progressbar-legacy
    AnimImageView
  • 原文地址:https://www.cnblogs.com/DreamHighWithMe/p/3374728.html
Copyright © 2020-2023  润新知