hdu 4405 Aeroplane chess(简单概率dp 求期望)

 

Aeroplane chess

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


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

 

 

题意:

有一个飞行棋n个格子,刚开始在0这个位置,每次可以扔色子,扔到x则可以移动x格
如果到达的位置>=n则胜利
另外在棋盘上还有m对点x,y表示在位置x可以直接飞行到y
求到达胜利平均的扔色子次数

分析:

期望逆推。

d[i]表示到达 i 的时候还需要几次才能到达n。

初始d[n] = 0; 而d[0]就是所求的答案

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <queue>
 6 #include <cmath>
 7 #include <algorithm>
 8 #define LL __int64
 9 const int maxn = 1e5+10;
10 using namespace std;
11 double d[maxn];
12 int c[maxn];
13 
14 int main()
15 {
16     int n, m, i, j;
17     while(~scanf("%d%d", &n, &m))
18     {
19         if(n==0 && m==0) break;
20         memset(c, -1, sizeof(c));
21         for(i = 0; i < m; i++)
22         {
23             int a, b;
24             scanf("%d%d", &a, &b);
25             c[a] = b;
26         }
27         memset(d, 0, sizeof(d));
28         d[n] = 0;
29         double t = 1.0/6;
30         for(i = n-1; i >= 0; i--)
31         {
32             if(c[i]==-1)
33             d[i] += 1.0 + d[i+1]*t + d[i+2]*t+d[i+3]*t+d[i+4]*t+d[i+5]*t+d[i+6]*t;
34             else
35             d[i] = d[c[i]];
36         }
37         printf("%.4f\n", d[0]);
38     }
39     return 0;
40 }

 

posted @ 2014-11-04 20:14  水门  阅读(166)  评论(0编辑  收藏  举报