Aeroplane chess

Time Limit: 1 Sec  Memory Limit: 32 MB
[Submit][Stataus][Discuss]

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.
  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

HINT

  1≤N≤100000, 0≤M≤1000

Main idea

  从0走到n-1,每次以均等概率走1~6步,某些点可以直接跨越到指定点,求走出n所需走的次数的期望。

Solution

  我们直接使用期望DP求解。令 f[i] 表示到位置 i 的期望,然后直接从后往前递推即可。

Code

  1. #include<iostream>
  2. #include<string>
  3. #include<algorithm>
  4. #include<cstdio>
  5. #include<cstring>
  6. #include<cstdlib>
  7. #include<cmath>
  8. #include<bitset>
  9. using namespace std;
  10. const int ONE = ;
  11.  
  12. int n,m;
  13. int x,y,To[ONE];
  14. double f[ONE];
  15.  
  16. int get()
  17. {
  18. int res=,Q=; char c;
  19. while( (c=getchar())< || c>)
  20. if(c=='-')Q=-;
  21. if(Q) res=c-;
  22. while((c=getchar())>= && c<=)
  23. res=res*+c-;
  24. return res*Q;
  25. }
  26.  
  27. void Solve()
  28. {
  29. n=get(); m=get();
  30. if(!n && !m) exit();
  31. memset(To,,sizeof(To)); memset(f,,sizeof(f));
  32. for(int i=;i<=m;i++)
  33. {
  34. x=get(); y=get();
  35. To[x] = y;
  36. }
  37.  
  38. for(int i=n-;i>=;i--)
  39. {
  40. if(To[i]) {f[i] = f[To[i]]; continue;}
  41. for(int j=;j<=;j++)
  42. f[i] += (double)/ * f[min(i+j,n)];
  43. f[i]++;
  44. }
  45.  
  46. printf("%.4lf\n",f[]);
  47. }
  48.  
  49. int main()
  50. {
  51. for(;;)
  52. Solve();
  53. }

【HDU4405】Aeroplane chess [期望DP]的更多相关文章

  1. HDU4405 Aeroplane chess(期望dp)

    题意 抄袭自https://www.cnblogs.com/Paul-Guderian/p/7624039.html 正在玩飞行棋.输入n,m表示飞行棋有n个格子,有m个飞行点,然后输入m对u,v表示 ...

  2. HDU 4405 Aeroplane chess 期望dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Time Limit: 2000/1000 MS (Java/ ...

  3. [hdu4405]Aeroplane chess(概率dp)

    题意:某人掷骰子,数轴上前进相应的步数,会有瞬移的情况,求从0到N所需要的期望投掷次数. 解题关键:期望dp的套路解法,一个状态可以转化为6个状态,则该状态的期望,可以由6个状态转化而来.再加上两个状 ...

  4. HDU4405 Aeroplane chess (概率DP,转移)

    http://acm.hdu.edu.cn/showproblem.php?pid=4405 题意:在一个1×n的格子上掷色子,从0点出发,掷了多少前进几步,同时有些格点直接相连,即若a,b相连,当落 ...

  5. 2018.09.01 hdu4405 Aeroplane chess (期望dp)

    传送门 期望dp简单题啊. 不过感觉题意不太对. 手过了一遍样例发现如果有捷径必须走. 这样的话就简单了啊. 设f[i]" role="presentation" sty ...

  6. HDU4405 Aeroplane chess 飞行棋 期望dp 简单

    http://acm.hdu.edu.cn/showproblem.php?pid=4405   题意:问从起点到终点需要步数的期望,1/6的概率走1.2.3.4.5.6步.有的点a有路可以直接到b, ...

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

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

  8. hdu4405 Aeroplane chess

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

  9. HDU-4405 Aeroplane chess

    http://acm.hdu.edu.cn/showproblem.php?pid=4405 看了一下这个博客http://kicd.blog.163.com/blog/static/12696191 ...

随机推荐

  1. volatility的使用

    volatility取证的使用----windows内存 简介 kali下默认安装 可以对windows,linux,mac,android的内存进行分析 内存文件的准备 Win2003SP2x86下 ...

  2. 生产者与消费者-N:1-基于list

    多个生产者/一个消费者: /** * 生产者 */ public class P { private MyStack stack; public P(MyStack stack) { this.sta ...

  3. 【SpringCloud】第一篇: 服务的注册与发现(Eureka)

    前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...

  4. zabbix从入门到精通

    第1章 zabbix监控 1.1 为什么要监控 在需要的时刻,提前提醒我们服务器出问题了 当出问题之后,可以找到问题的根源   网站/服务器 的可用性 1.1.1 网站可用性 在软件系统的高可靠性(也 ...

  5. 参加2018之江杯全球人工智能大赛 :视频识别&问答(四)

    很遗憾没有在规定的时间点(2018-9-25 12:00:00)完成所有的功能并上传数据,只做到写了模型代码并只跑了一轮迭代,现将代码部分贴出. import keras from keras.lay ...

  6. java笔试面试01

    今天给大家分享一下小布去广州华南资讯科技公司笔试和面试的过程. 过程:1.HR面试  2.笔试  3.技术面试 小布下午两点到达,进门从前台领了一张申请表,填完之后带上自己的简历到4楼就开始HR面试. ...

  7. 【SSH】——Hibernate三种状态之间的转化

    Hibernate的三种状态为:transient.persistent和detached.对这三种状态的理解可以结合Session缓存,在Session缓存中的状态为persistent,另外两种不 ...

  8. 转:maven常用命令介绍

    mvn 3.0.4 创建maven项目命令  mvn  archetype:generate   -DgroupId=damocles-autocredit -DartifactId=damocles ...

  9. ECharts饼图制作分析

    ECharts,缩写来自Enterprise Charts,商业级数据图表,一个纯Javascript的图表库,可以流畅的运行在PC和移动设备上,兼容当前绝大部分浏览器(IE6/7/8/9/10/11 ...

  10. 附录A培训实习生-面向对象基础方法重载(3)

    就上一篇代码而言,你如果写Cat cat = new Cat();会直接报错错误 : 1       “Cat”方法没有采用“0”个参数的重载 E:\大话设计模式学习\BigDesignPattern ...