hdu 4405Aeroplane chess(概率DP)

Aeroplane chess

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1581    Accepted Submission(s): 1082

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

有一个长条形棋盘,每一次可以跳1,2,3,4,5,6中的一种的距离,且每一跳的概率相等1/6;且在某一个点出有一个航班可以帮助他不需要跳直接可以达到y处

问到n平均需要跳多少次。

dp【n】=0;因为在哪里不需要再跳,这个已知,所以可以倒退

dp[i]=sum{1+dp[n+j]}(1<=j<=6)

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<stack>
 4 #define maxn 100008
 5 using namespace std;
 6 double  dp[maxn];
 7 stack<int> path[maxn];
 8 int pa[maxn];
 9 int main()
10 {
11   int n,m,a,b;
12   while(scanf("%d%d",&n,&m)!=EOF&&n+m)
13   {
14     memset(pa,0,sizeof(int)*(n+2));
15     memset(dp,0,sizeof(double)*(n+8));
16       while(m--)
17     {
18       scanf("%d%d",&a,&b);
19       path[b].push(a);
20       pa[a]=b;
21     }
22     while(--n>=0)
23     {
24       while(!path[n+1].empty())
25      {
26          dp[path[n+1].top()]=dp[n+1];
27          path[n+1].pop();
28      }
29       if(pa[n]==0)
30          dp[n]=1.0+(dp[n+1]+dp[n+2]+dp[n+3]+dp[n+4]+dp[n+5]+dp[n+6])/6.0;
31     }
32    printf("%.4lf\n",dp[0]);
33   }
34   return 0;
35 }

时间: 2024-10-24 14:23:02

hdu 4405Aeroplane chess(概率DP)的相关文章

[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 4832 Chess (DP)

Chess Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 24    Accepted Submission(s): 10 Problem Description 小度和小良最近又迷上了下棋.棋盘一共有N行M列,我们可以把左上角的格子定为(1,1),右下角的格子定为(N,M).在他们的规则中,"王"在棋盘上的走法遵循十字路线.

hdu 4870 Rating(概率DP&amp;高数消元)

Rating Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 714    Accepted Submission(s): 452 Special Judge Problem Description A little girl loves programming competition very much. Recently, she

HDU 4035Maze(概率DP)

HDU 4035   Maze 体会到了状态转移,化简方程的重要性 题解转自http://blog.csdn.net/morgan_xww/article/details/6776947 /** dp求期望的题. 题意: 有n个房间,由n-1条隧道连通起来,实际上就形成了一棵树, 从结点1出发,开始走,在每个结点i都有3种可能: 1.被杀死,回到结点1处(概率为ki) 2.找到出口,走出迷宫 (概率为ei) 3.和该点相连有m条边,随机走一条 求:走出迷宫所要走的边数的期望值. 设 E[i]表示

HDU 3853 LOOPS (概率dp)

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

HDU 4089 Activation (概率dp 好题 + 难题)

Activation Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1842    Accepted Submission(s): 689 Problem Description After 4 years' waiting, the game "Chinese Paladin 5" finally comes out.

HDU - 1099 - Lottery - 概率dp

http://acm.hdu.edu.cn/showproblem.php?pid=1099 最最简单的概率dp,完全是等概率转移. 设dp[i]为已有i张票,还需要抽几次才能集齐的期望. 那么dp[n]=0,因为我们已经集齐了. \[dp[i]=(\frac{i}{n}*dp[i]+\frac{n-i}{n}*dp[i+1])+1\] 移项得答案. 然后写个分数类,注意约分. #include<bits/stdc++.h> using namespace std; typedef long

HDU 4599 Dice (概率DP+数学+快速幂)

题意:给定三个表达式,问你求出最小的m1,m2,满足G(m1) >= F(n), G(m2) >= G(n). 析:这个题是一个概率DP,但是并没有那么简单,运算过程很麻烦. 先分析F(n),这个用DP来推公式,d[i],表示抛 i 次连续的点数还要抛多少次才能完成.那么状态转移方程就是 d[i] = 1/6*(1+d[i+1]) + 5/6*(1+d[1]), 意思就是说在第 i 次抛和上次相同的概率是1/6,然后加上上次抛的和这一次,再加上和上次不同的,并且又得从第1次开始计算. 边界就是

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