hdu 4405(概率dp简单题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4405

Aeroplane chess

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

Total Submission(s): 1535    Accepted Submission(s): 1050

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

思路:用dp[i]表示从i这点出发到游戏结束的 expect 步数,

那么容易知道状态转移方程式:

p表示概率;

dp[i]=p*dp[i+1]+p*dp[i+2]+p*dp[i+3]+p*dp[i+4]+p*dp[i+5]+p*dp[i+6]+1;

#include <iostream>
#include <string.h>
#include <string>
#include <cstdio>
#include <algorithm>
#include <map>
const int N=1e5+100;
using namespace std;
double dp[N];

int main()
{
    int n,m;
    map<int ,int >mp;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
      if(n==0&&m==0)break;
      mp.clear();
      memset(dp,0,sizeof(dp));
      for(int i=0;i<m;i++)
      {
        int a,b;
        cin>>a>>b;
        mp[a]=b;
      }
      double p=1.0/6;
      for(int i=n;i>=0;i--)
      {
        if(i==n)continue;
        if(mp[i])
          dp[i]=dp[mp[i]];
        else
        dp[i]=p*dp[i+1]+p*dp[i+2]+p*dp[i+3]+p*dp[i+4]+p*dp[i+5]+p*dp[i+6]+1;
      }
     printf("%.4lf\n",dp[0]);
    }
    return 0;
}
时间: 2024-08-01 22:46:08

hdu 4405(概率dp简单题)的相关文章

hdu 3853 概率DP 简单

http://acm.hdu.edu.cn/showproblem.php?pid=3853 题意:有R*C个格子,一个家伙要从(0,0)走到(R-1,C-1) 每次只有三次方向,分别是不动,向下,向右,告诉你这三个方向的概率,以及每走一步需要耗费两个能量,问你走到终点所需要耗费能量的数学期望: 回头再推次,思想跟以前的做过的类似 注意点:分母为0的处理 #include <cstdio> #include <cstring> #include <algorithm>

hdu 4405 概率dp 2012年金华亚洲网络赛--虽然水,但是是自己独立做的第一道概率dp

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4405 e[i]:当前在位置i还需要走的步数期望 受刘汝佳的AC自动机那个后缀链接写法的启发,我的x[i]通过逆序算出来连续有"flight line "的时候,能到达的最远距离, rep(i,0,m) { scanf("%d%d",&xx,&yy); x[xx]=yy; } for(int i=n;i>=0;i--) if(x[i]!=-1 &

HDU 4405 (概率DP)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4405 题目大意:飞行棋.如果格子不是飞行点,扔骰子前进.否则直接飞到目标点.每个格子是唯一的飞行起点,但不是唯一的飞行终点.问到达或越过终点的扔骰子期望数. 解题思路: 一个告诉你求期望应该逆推而不是正推的题. 如果正推的话,对于一个点i,如果是飞行终点,那么势必要枚举到达它的飞行起点,起点有多个,每个起点概率不一定相等,期望怎么求? 如果逆推(终点变成起点)的话,对于一个点i,如果是飞行起点,那

hdu 4405 概率dp求期望

题意是给你n+1个点 标号是0-n   现在按投掷色子的方式走   求走到n点以后的次数的期望, dp[i]表示从i到终点的期望      开始dp[n]=0,然后从后网前求 如果该点不能飞则dp[i]=∑dp[i+k]*1/6+1:  最后输出dp[0]即可:  否则dp[i]=dp[map[i]]: #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> us

概率DP入门题

一 概率问题的论文 1.算法合集之<信息学竞赛中概率问题求解初探> 2.有关概率和期望问题的研究 3.算法合集之<浅析竞赛中一类数学期望问题的解决方法> 二 入门题目 1.POJ 3744 Scout YYF I (简单题) 题意:一条路上有n个地雷 ,a[i]代表第i个地雷放的位置,求安全走过这段路的概率 分析:若第k个位置有地雷则安全走过这个位置的方案为在第k-1个位置跳两步概率为(1-p) 从反面考虑 已经安全走过了第i-1个雷 则在第i个雷的死掉的概率为 1-p(从走到a[

hdu 5001 walk 概率dp入门题

Description I used to think I could be anything, but now I know that I couldn't do anything. So I started traveling. The nation looks like a connected bidirectional graph, and I am randomly walking on it. It means when I am at node i, I will travel t

HDU 3853 概率dp

LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Submission(s): 2337    Accepted Submission(s): 951 Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl).Homura wants to help he

13年山东省赛 The number of steps(概率dp水题)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud The number of steps Time Limit: 1 Sec  Memory Limit: 128 M Description Mary stands in a strange maze, the maze looks like a triangle(the first layer have one room,the second layer have two ro

hdu 1312 DFS算法简单题

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 此题与油田那题很像是练习深搜的好题,题意是从"@"开始,遍历整个图,找到连接的 "."有多少个 但要考虑变化,简单处理下就行了,主要是斜角的不要了,而且判断结束方式也不一样 具体看代码吧 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32