zoukankan      html  css  js  c++  java
  • HDU-4405-期望dp

    Aeroplane chess

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


    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
     
        一个人从0号格子走到N号格子以后摇动骰子的次数的期望,期间有一些特殊的格子可以直接跳到后面的某个格子不必摇动骰子。
        不算很难,方程 f[i]= f[i+v[i]]   ///if(v[i]!=-1)
               f[i]=1+1/6*(f[i+k]) | 1<=k<=6   ///else
     1 #include<iostream>
     2 #include<cstring>
     3 #include<queue>
     4 #include<cstdio>
     5 #include<stack>
     6 #include<set>
     7 #include<map>
     8 #include<cmath>
     9 #include<ctime>
    10 #include<time.h> 
    11 #include<algorithm>
    12 using namespace std;
    13 #define mp make_pair
    14 #define pb push_back
    15 #define debug puts("debug")
    16 #define LL long long 
    17 #define pii pair<int,int>
    18 #define eps 1e-12
    19 
    20 double f[100110];
    21 int v[100110];
    22 int main()
    23 {
    24     int n,m,i,j,k,t;
    25     while(scanf("%d%d",&n,&m)&&(n||m)){
    26         memset(v,-1,sizeof(v));
    27         memset(f,0,sizeof(f));
    28         double p=(double)1.0/6;
    29         int Y,X;
    30         while(m--){
    31             scanf("%d%d",&X,&Y);
    32             v[X]=Y;
    33         }
    34         for(i=n-1;i>=0;--i){
    35             if(v[i]!=-1){
    36                 f[i]=f[v[i]];
    37             }
    38             else{
    39                 double res=0;
    40                 for(j=1;j<=6;++j) res+=f[i+j];
    41                 f[i]=1.0+p*res;
    42             }
    43         }
    44         printf("%.4f
    ",f[0]);
    45     }
    46     return 0; 
    47 }
  • 相关阅读:
    DFS复习
    二叉搜索树专题
    二叉树路径问题
    二叉树LCA--leetcode236题
    二叉树创建与前、中、后序遍历
    leetCode--n数之和--哈希表/双指针
    leetCode--单词接龙--BFS
    vue-router简单实现
    Promise的简单实现
    闭包&作用域链&let
  • 原文地址:https://www.cnblogs.com/zzqc/p/8984014.html
Copyright © 2011-2022 走看看