zoukankan      html  css  js  c++  java
  • HDU4405-Aeroplane chess(概率DP求期望)

    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. 

    InputThere 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. 
    OutputFor 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,掷到多少就向前走几步,这个数轴上还有些特殊点,这些点类似
    飞行棋中的飞行点,只要到达这些点就可以直接飞到给定点。求总共投掷骰子次数的期望。

    题解:如果直接可以飞行就f[i]=f[reach],然后就是递推吧。
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<cmath>
    #define ll long long
    #define N 100007
    
    int n,m;
    double f[N];
    int go_to[N];
    
    int main()
    {
        while(~scanf("%d%d",&n,&m)&&(n+m))
        {
            memset(go_to,-1,sizeof(go_to));
            memset(f,0,sizeof(f));
            int x,y;
            for(int i=1;i<=m;i++)
            {
                scanf("%d%d",&x,&y);
                go_to[x]=y;
            }
            for(int i=n-1;i>=0;i--)
            {
                if(go_to[i]==-1)
                {
                    for(int j=1;j<=6;j++){
                        f[i]+=f[i+j]/6.0;
                    }
                    f[i]+=1;
                }
                else f[i]=f[go_to[i]];
            }
            printf("%.4lf
    ",f[0]);
        }
    }


  • 相关阅读:
    关于Log4j的初始化
    Golang-interface(四 反射)
    JavaScript学习总结-技巧、有用函数、简洁方法、编程细节
    玩转iOS开发
    小谈一下Java I/O
    [ACM] 最短路算法整理(bellman_ford , SPFA , floyed , dijkstra 思想,步骤及模板)
    已超过了锁请求超时时段。 (Microsoft SQL Server,错误: 1222)
    计数排序
    跟我学solr---吐槽一下,我的文章被抄袭啦
    Navicat11全系列激活工具和使用方法
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/7673883.html
Copyright © 2011-2022 走看看