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): 3970    Accepted Submission(s): 2537


    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,置骰子(6个面),置到几就往前走几步,但图中有可选传送门,比 如2到5有传送门的话,那你走到2时可以直接跳到5(也可以不跳),如果5到8也有传送门的话,那 还可以继续跳到攸,最后问从0到n的期望步数!(传送门一定是从小的点到大的点,且一个点最多为 一个传送门的起点)
    题解:如果没有传送门的话问题会非常清楚,f[i]表示在第i格走到第n格期望步数,f[i]=1+f[j]/6  (i<j<=i+6),传送门 i->j 相当于 f[i]=f[j];
    //参考 dp进阶之路
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<vector>
     5 using namespace std;
     6 double f[200005];
     7 vector<int> mp[200005];
     8 int v[200005];
     9 int n,m;
    10 int main()
    11 {
    12     while(scanf("%d %d",&n,&m)!=EOF)
    13    {
    14        if(n==0&&m==0)
    15        break;
    16        for(int i=0;i<=n;i++) mp[i].clear();
    17        memset(f,0,sizeof(f));
    18        for(int i=1;i<=m;i++)
    19        {
    20         int x,y;
    21         scanf("%d %d",&x,&y);
    22         mp[y].push_back(x);
    23        }
    24        memset(v,0,sizeof(v));
    25        for(int i=0;i<mp[n].size();i++)
    26         v[mp[n][i]]=1;
    27        for(int i=n-1;i>=0;i--)
    28        {
    29            if(v[i]==0)
    30            {
    31                for(int j=1;j<=6;j++)
    32                 f[i]+=f[i+j]/6;
    33                f[i]+=1;
    34                v[i]=1;
    35            }
    36            for(int j=0;j<mp[i].size();j++)
    37            {
    38                f[mp[i][j]]=f[i];
    39                v[mp[i][j]]=1;
    40            }
    41        }
    42          printf("%.4f
    ",f[0]);
    43    }
    44     return 0;
    45 }
  • 相关阅读:
    Codeforces Round #604 (Div. 2) E. Beautiful Mirrors 题解 组合数学
    洛谷P1002 过河卒 题解 动态规划
    python 查看数据类型
    python 输出小技巧
    python 基础定义变量
    python 关键字
    flask restful 反查询
    Please edit configuration/connection/logging settings in '/home/mac126/Desktop/13/test3-1/migrations/alembic.ini' before proceeding.
    KeyError: 'migrate'错误解决办法
    flask 的简单拆分升级版2
  • 原文地址:https://www.cnblogs.com/hsd-/p/6715573.html
Copyright © 2011-2022 走看看