题目链接:

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)
#### 问题描述
> 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
> Please help Hzz calculate the expected dice throwing times to finish the game.
#### 输入
> 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 The input end with N=0, M=0.

输出

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.

样例输入

2 0

8 3

2 4

4 5

7 8

0 0

样例输出

1.1667

2.3441

题意

飞行棋,每次投掷骰子,(1-6等概率),按骰子大小前进,其中有若干个飞机场(a,b),可以从a直接传送到b,现从0点出发,问到达>=N的点需要的期望步数。

题解

期望dp入门:

全期望公式:E[x]=sigma(pi*E[xi]).

有飞机场的话:E[a]=E[b]

否则:E[i]=sigma(1/6*(E[i+x]+1)).

代码

  1. #include<map>
  2. #include<set>
  3. #include<cmath>
  4. #include<queue>
  5. #include<stack>
  6. #include<ctime>
  7. #include<vector>
  8. #include<cstdio>
  9. #include<string>
  10. #include<bitset>
  11. #include<cstdlib>
  12. #include<cstring>
  13. #include<iostream>
  14. #include<algorithm>
  15. #include<functional>
  16. using namespace std;
  17. #define X first
  18. #define Y second
  19. #define mkp make_pair
  20. #define lson (o<<1)
  21. #define rson ((o<<1)|1)
  22. #define mid (l+(r-l)/2)
  23. #define sz() size()
  24. #define pb(v) push_back(v)
  25. #define all(o) (o).begin(),(o).end()
  26. #define clr(a,v) memset(a,v,sizeof(a))
  27. #define bug(a) cout<<#a<<" = "<<a<<endl
  28. #define rep(i,a,b) for(int i=a;i<(b);i++)
  29. #define scf scanf
  30. #define prf printf
  31. typedef long long LL;
  32. typedef vector<int> VI;
  33. typedef pair<int,int> PII;
  34. typedef vector<pair<int,int> > VPII;
  35. const int INF=0x3f3f3f3f;
  36. const LL INFL=0x3f3f3f3f3f3f3f3fLL;
  37. const double eps=1e-8;
  38. const double PI = acos(-1.0);
  39. //start----------------------------------------------------------------------
  40. const int maxn=101010;
  41. ///dp[i]表示在i点,到达终点还需几步
  42. ///期望一般逆推求,比如这一题,你已知的状态是终点而不是起点。
  43. double dp[maxn];
  44. int mp[maxn];
  45. int n,m;
  46. void init(){
  47. clr(mp,-1);
  48. clr(dp,0);
  49. }
  50. int main() {
  51. while(scf("%d%d",&n,&m)==2&&n){
  52. init();
  53. rep(i,0,m){
  54. int u,v;
  55. scf("%d%d",&u,&v);
  56. mp[u]=v;
  57. }
  58. for(int i=n-1;i>=0;i--){
  59. ///有飞机场,直接飞过去
  60. if(mp[i]!=-1){
  61. dp[i]=dp[mp[i]];
  62. continue;
  63. }
  64. ///掷骰子,全期望公式
  65. for(int x=1;x<=6;x++){
  66. dp[i]+=1.0/6*(dp[i+x]+1);
  67. }
  68. }
  69. prf("%.4lf\n",dp[0]);
  70. }
  71. return 0;
  72. }
  73. //end-----------------------------------------------------------------------

HDU 4405 Aeroplane chess 期望dp的更多相关文章

  1. [ACM] hdu 4405 Aeroplane chess (概率DP)

    Aeroplane chess Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 ...

  2. HDU 4405 Aeroplane chess(概率dp,数学期望)

    题目 http://kicd.blog.163.com/blog/static/126961911200910168335852/ 根据里面的例子,就可以很简单的写出来了,虽然我现在还是不是很理解为什 ...

  3. HDU 4405 Aeroplane chess 概率DP 难度:0

    http://acm.hdu.edu.cn/showproblem.php?pid=4405 明显,有飞机的时候不需要考虑骰子,一定是乘飞机更优 设E[i]为分数为i时还需要走的步数期望,j为某个可能 ...

  4. HDU 4405 Aeroplane chess (概率DP)

    题意:你从0开始,要跳到 n 这个位置,如果当前位置是一个飞行点,那么可以跳过去,要不然就只能掷骰子,问你要掷的次数数学期望,到达或者超过n. 析:概率DP,dp[i] 表示从 i  这个位置到达 n ...

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

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  6. hdu 4405 Aeroplane chess (概率DP)

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

  7. hdu 4405 Aeroplane chess(概率+dp)

    Problem Description Hzz loves aeroplane chess very much. The chess map contains N+ grids labeled to ...

  8. 【HDU4405】Aeroplane chess [期望DP]

    Aeroplane chess Time Limit: 1 Sec  Memory Limit: 32 MB[Submit][Stataus][Discuss] Description Hzz lov ...

  9. 【刷题】HDU 4405 Aeroplane chess

    Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled fr ...

随机推荐

  1. Go压缩文件

    Go压缩文件 首先是恭喜IG获得S8全球总决赛冠军,IG牛逼.但咱是一介草民,狂欢后,还是得老老实实的开始敲代码.最近做了一个给底层固件压缩加密的工具,是使用C#做的,已经提交出去可以正常使用的.既然 ...

  2. c# multi-ply download ui

    first neet add an user control "DownloadBar": /* Created by SharpDevelop. User: gwang Date ...

  3. 20155238 2016-2017-2 《Java程序设计》第三周学习总结

    教材学习内容总结 类定义使用class关键词,名称使用Cloths,建立实例运用New关键词 Clothes c1 = new Clothes(); = :制定参考名称参考某个对象 == :比较参考名 ...

  4. Noip前的大抱佛脚----根号对数算法

    根号算法 分块 数列分块入门九题(hzwer) 入门题1,2,3,4,5,7 问题:给一段区间打上标记后单点查询 解法:主要是每块维护一些标记,计算答案等,此类分块较为简单 注意:块大小一般为\(\s ...

  5. 6-[多线程]-互斥锁、GIL、死锁、递归锁、信号量

    1.互斥锁(排他锁) (1)不加锁的情况下 并发控制问题:多个事务并发执行,可能产生操作冲突,出现下面的3种情况 丢失修改错误 不能重复读错误 读脏数据错误 # mutex from threadin ...

  6. 23-[jQuery]-效果:隐藏,淡出,盒子高度,动画

    1.隐藏,显示 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  7. 1、rbac权限组件-初识, 中间件校验1

    1.权限组件rbac 1.什么是权限 1 项目与应用 2 什么是权限? 一个包含正则表达式url就是一个权限 who what how ---------->True or Flase 2.版本 ...

  8. [NOI2007]货币兑换 cdq分治,斜率优化

    [NOI2007]货币兑换 LG传送门 妥妥的\(n \log n\)cdq做法. 这题用cdq分治也可以\(n \log n\)但是在洛谷上竟然比一些优秀的splay跑得慢真是见了鬼了看来还是人丑常 ...

  9. C#面试题及答案 一 <转来的,貌似有看评论说有错误,正在一个个纠正中…… 也望园友们指出>

    1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private  私有成员, 在类的内部才可以访问.  protected  保护成员 ...

  10. 使用 Python+Selenium 破解滑块验证码

    ​​开发工具 Python版本:3.6.4 相关模块: pillow模块: selenium模块: numpy模块: 以及一些Python自带的模块. 其他: chromedriver 环境搭建 安装 ...