zoukankan      html  css  js  c++  java
  • hdu 4405 Aeroplane chess(概率+dp)

    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
     

     这道题适合从后面开始dp。将能跳的格子标志了,然后从n-1开始dp。具体看代码吧,不知道怎么说了。。。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 #define N 100006
     6 int n,m;
     7 double dp[N];
     8 int vis[N];
     9 int main()
    10 {
    11     while(scanf("%d%d",&n,&m)==2 && n+m){
    12         memset(vis,-1,sizeof(vis));
    13         memset(dp,0,sizeof(dp));
    14         for(int i=0;i<m;i++){
    15             int x,y;
    16             scanf("%d%d",&x,&y);
    17             vis[x]=y;
    18         }
    19         for(int i=n-1;i>=0;i--){
    20             if(vis[i]!=-1){
    21                 dp[i]=dp[vis[i]];
    22             }else{
    23                 for(int j=1;j<=6;j++){
    24                     dp[i]+=dp[i+j]/6;
    25                 }
    26                 dp[i]+=1.0;
    27             }
    28         }
    29         printf("%.4lf
    ",dp[0]);
    30     }
    31     return 0;
    32 }
    View Code
  • 相关阅读:
    边框的各种样式
    内容溢出显示省略号
    UNIAPP开发注意事项
    css文本的三条线 删除线 下划线 上划线
    防抖截流
    浏览器窗口改变触发的函数
    ES5数组方法
    弹性布局
    ubuntu16.04 安装adb
    git clone
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4791360.html
Copyright © 2011-2022 走看看