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]);
        }
    }


  • 相关阅读:
    VS2010/MFC编程入门之四(MFC应用程序框架分析)
    VS2010/MFC编程入门之三(VS2010应用程序工程中文件的组成结构)
    shell脚本学习一
    docker安装rabbitmq
    docker安装activemq
    docker搭建数据库主从复制
    在docker容器中安装vim命令进行编辑文件
    docker的常用操作
    docker上安装eslaticsearch
    centos7上面关闭防火墙
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/7673883.html
Copyright © 2011-2022 走看看