hdu 4405 概率DP

Aeroplane chess

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


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的n+1个格子,起始位置为0,每次摇骰子摇到几就走几步,有m个跳越,就跟自己玩的时候刚好走到与自己飞机颜色相同的时候可以跳。求到>=n位置摇骰子次数的期望。

解题思路:无环有限次递推,用递推或递归记忆化搜索。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 const int maxn=100010;
 7 double p[maxn],e[maxn];
 8 int n,m;
 9 int f[maxn];
10 
11 int main()
12 {
13     int i,j;
14     while(scanf("%d%d",&n,&m),n+m)
15     {
16         memset(p,0,sizeof(p));
17         memset(e,0,sizeof(e));
18         memset(f,-1,sizeof(f));
19         for(i=0;i<m;i++)
20         {
21             int x,y;
22             scanf("%d%d",&x,&y);
23             f[x]=y;
24         }
25         p[0]=1;
26         for(i=0;i<n;i++)
27         {
28             if(f[i]==-1)
29             {
30                 for(j=1;j<=6;j++)
31                 {
32                     p[i+j]+=p[i]/6;
33                     e[i+j]+=(p[i]+e[i])/6;
34                 }
35             }
36             else
37             {
38                 p[f[i]]+=p[i];e[f[i]]+=e[i];
39             }
40         }
41         double ans=0;
42         for(i=0;i<6;i++)
43             ans+=e[n+i];
44         printf("%.4lf\n",ans);
45     }
46     return 0;
47 }

 

posted on 2014-08-15 15:58  雄..  阅读(133)  评论(0编辑  收藏  举报

导航