码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 4405 概率DP

时间:2014-08-15 17:20:19      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   java   os   io   

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 }

 

hdu 4405 概率DP,布布扣,bubuko.com

hdu 4405 概率DP

标签:des   style   blog   http   color   java   os   io   

原文地址:http://www.cnblogs.com/xiong-/p/3915096.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!