zoukankan      html  css  js  c++  java
  • 【HDU4405】Aeroplane chess [期望DP]

    Aeroplane chess

    Time Limit: 1 Sec  Memory Limit: 32 MB
    [Submit][Stataus][Discuss]

    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.
      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

    HINT

      1≤N≤100000, 0≤M≤1000

    Main idea

      从0走到n-1,每次以均等概率走1~6步,某些点可以直接跨越到指定点,求走出n所需走的次数的期望。

    Solution

      我们直接使用期望DP求解。令 f[i] 表示到位置 i 的期望,然后直接从后往前递推即可。

    Code

     1 #include<iostream>  
     2 #include<string>  
     3 #include<algorithm>  
     4 #include<cstdio>  
     5 #include<cstring>  
     6 #include<cstdlib>  
     7 #include<cmath>
     8 #include<bitset>
     9 using namespace std; 
    10 const int ONE = 100001;
    11 
    12 int n,m;
    13 int x,y,To[ONE];
    14 double f[ONE];
    15 
    16 int get() 
    17 {
    18         int res=1,Q=1;  char c;
    19         while( (c=getchar())<48 || c>57)
    20         if(c=='-')Q=-1;
    21         if(Q) res=c-48; 
    22         while((c=getchar())>=48 && c<=57) 
    23         res=res*10+c-48; 
    24         return res*Q; 
    25 }
    26 
    27 void Solve()
    28 {
    29         n=get();    m=get();
    30         if(!n && !m) exit(0);
    31         memset(To,0,sizeof(To)); memset(f,0,sizeof(f));
    32         for(int i=1;i<=m;i++)
    33         {
    34             x=get();    y=get();
    35             To[x] = y; 
    36         }
    37         
    38         for(int i=n-1;i>=0;i--)
    39         {
    40             if(To[i]) {f[i] = f[To[i]]; continue;}
    41             for(int j=1;j<=6;j++)
    42                 f[i] += (double)1/6 * f[min(i+j,n)];
    43             f[i]++;
    44         }
    45         
    46         printf("%.4lf
    ",f[0]);
    47 }
    48 
    49 int main()
    50 {
    51         for(;;)
    52             Solve();
    53 }
    View Code

     

  • 相关阅读:
    如何获得Spring容器里管理的Bean,。不论是Service层,还是实体Dao层
    解析PHP中的file_get_contents获取远程页面乱码的问题【转】
    CSS中应用position的absolute和relative的属性制作浮动层
    css position 绝对定位和相对定位
    html bootstrap 表头固定在顶部,表列 可以自由滚动的效果
    php工具 phpstorm 的快捷键 的使用(待添加
    关于PHP HTML <input type="file" name="img"/>上传图片,图片大小,宽高,后缀名。
    Thinkphp 3.2 添加 验证码 如何添加。
    网页自适应@media
    如何让div上下左右都居中
  • 原文地址:https://www.cnblogs.com/BearChild/p/6646805.html
Copyright © 2011-2022 走看看