HDU 4403(Aeroplane chess ,求期望,概率DP)

Aeroplane chess

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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

2012 ACM/ICPC Asia Regional Jinhua Online

Recommend

zhoujiaqi2010

题目大意:

飞行棋。给一组数据 N,M ,N代表有N+1(一维,0->N)个格子,你的起始点是在0号位置,M代表你有M条航班,接下来会有M行,每行两个整数X,Y,表示在位置X和位置Y有一条航班,可以直接从X飞到Y,投掷一枚骰子,投掷多少就能走多少步,如遇到航班,则按照航班走,航班可以连续,每个航班的起始点不同。输出投掷色子次数的期望。

解题思路:

dp [ n ]=0,dp [ i ]=sum( dp [i+j] ) +1, j 从1累加到6,因为期望代表的是步数,所以每次加 1 步,当遇到航班 (x,y)时,则记dp [ x ] = dp [ y ] ,则结果等于dp [0].

代码:

#include<iostream>
#include<cstdio>

using namespace std;

const int maxN=100010;

int main(){
    int a,b,n,m,visited[maxN];
    double dp[maxN];
    while(~scanf("%d%d",&n,&m)&&(n||m)){
        for(int i=0;i<n;i++){
            visited[i]=-1;
        }
         for(int i=0;i<=n+5;i++){
            dp[i]=0;
        }
        for(int i=0;i<m;i++){
            scanf("%d%d",&a,&b);
            visited[a]=b;
        }
        for(int i=n-1;i>=0;i--){
            if(visited[i]==-1){
                for(int j=1;j<=6;j++){
                    dp[i]=dp[i+j]/6.0+dp[i];
                }
                dp[i]+=1;
            }
            else dp[i]=dp[visited[i]];

        }
        printf("%.4lf\n",dp[0]);
    }
    return 0;
}
时间: 2024-10-10 21:32:36

HDU 4403(Aeroplane chess ,求期望,概率DP)的相关文章

HDU - 4405 Aeroplane chess(期望dp)

题意:沿着x轴从0走到大于等于N的某处,每一步的步数由骰子(1,2,3,4,5,6)决定,若恰好走到x轴上某飞行路线的起点,则不计入扔骰子数.问从0走到大于等于N的某处的期望的扔骰子次数. 分析: 1.dp[i]表示从位置i到终点期望的扔骰子次数. 2.很显然倒着往前推,因为从起点0开始,扔骰子的次数有很多种可能,难以计算,但是dp[N]很显然是0,不需要扔骰子即可到达终点. 3.假设当前位于位置i,根据骰子数可能到达的位置有i + j(j=1,2,3,4,5,6),到达其中每个位置的概率都是1

HDU 4405 Aeroplane chess (概率DP求期望)

题意:有一个n个点的飞行棋,问从0点掷骰子(1~6)走到n点需要步数的期望 其中有m个跳跃a,b表示走到a点可以直接跳到b点. dp[ i ]表示从i点走到n点的期望,在正常情况下i点可以到走到i+1,i+2,i+3,i+4,i+5,i+6 点且每个点的概率都为1/6 所以dp[i]=(dp[i+1]+dp[i+2]+dp[i+3]+dp[i+4]+dp[i+5]+dp[i+6])/6  + 1(步数加一). 而对于有跳跃的点直接为dp[a]=dp[b]; #include<stdio.h>

[ACM] hdu 4405 Aeroplane chess (概率DP)

Aeroplane chess 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 number

HDU 3853 期望概率DP

期望概率DP简单题 从[1,1]点走到[r,c]点,每走一步的代价为2 给出每个点走相邻位置的概率,共3中方向,不动: [x,y]->[x][y]=p[x][y][0] ,  右移:[x][y]->[x][y+1]=p[x][y][1];  左移:[x][y]->[x+1][y]=p[x][y][2]; 问最后走到[r,c]的期望 dp[i][j]为从[i][j]点走到[r][c]的期望 有方程: dp[i][j]=    (dp[i][j]+2)*p[i][j][0]  +   (dp

hdu 4405 Aeroplane chess

题意: hzz一开始在0位置,然后hzz掷骰子,骰子为i,就往前走i步,当hzz位置大于等于n的时候结束,求掷骰子次数的期望 有m个直达点 (x,y),走到x时可以直接到y 求期望一般从后往前推 当 i不等于任何一个x时 dp[i]=seg(1/6*dp[i+k])+1 否则 dp[i]=dp[y] 1 #include<iostream> 2 #include<string> 3 #include<cstdio> 4 #include<vector> 5

hdu 4865 Peter&#39;s Hobby(概率dp)

http://acm.hdu.edu.cn/showproblem.php?pid=4865 大致题意:有三种天气和四种叶子状态.给出两个表,分别是每种天气下叶子呈现状态的概率和今天天气对明天天气的概率.给出n天叶子的状态,输出最有可能的天气序列. 思路:wl[i][j]表示天气为i,叶子为j的概率,ww[i][j]表示今天天气为i明天天气为j的概率,st[i]表示第一天天气为i的概率. 对于叶子序列{a1,a2......an},存在一个天气序列{b1,b2......bn},那么总的概率g[

HDU 4336 Card Collector(动态规划-概率DP)

Card Collector Problem Description In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, for example, if you collect all the 108 people in the famous novel Water Margin, you will win an amazing award. As a

POJ3682King Arthur&#39;s Birthday Celebration(数学期望||概率DP)

King Arthur is an narcissist who intends to spare no coins to celebrate his coming K-th birthday. The luxurious celebration will start on his birthday and King Arthur decides to let fate tell when to stop it. Every day he will toss a coin which has p

【NOIP模拟赛】黑红树 期望概率dp

这是一道比较水的期望概率dp但是考场想歪了.......我们可以发现奇数一定是不能掉下来的,因为若奇数掉下来那么上一次偶数一定不会好好待着,那么我们考虑,一个点掉下来一定是有h/2-1个红(黑),h/2+1个黑(红),而且一定是差不多相间的(我就是因为没有看出来这里才会去想组合数,然后......),那么我们发现只要一奇一偶,就可以组成一对,因为偶数一定是平的因此,我们发现在掉下来的那对之前都是红黑或黑红,但是到了这里就是红红或黑黑了,我们只要求出(异色的概率)^(h/2-1)*(同色的概率)就

【BZOJ 3652】大新闻 数位dp+期望概率dp

并不难,只是和期望概率dp结合了一下.稍作推断就可以发现加密与不加密是两个互相独立的问题,这个时候我们分开算就好了.对于加密,我们按位统计和就好了;对于不加密,我们先假设所有数都找到了他能找到的最好的匹配(就是异或后为二进制最高位与n-1相等的最大数)并且算出其异或后的总和,然后我们按位贪心,带着所有的数(一开始我们假设所有的数是小于等于二进制最高位与n-1相等的最大数的所有数)从高位走向低位,每走一步,如果这一位是0,就会导致一半的数在这一位不能是1,减去这一半的数在这一位上的贡献,如果这一位