Aeroplane chess

Time Limit: 2000/1000 MS (Java/Others)

Memory Limit: 32768/32768 K (Java/Others)

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


解题心得:

  1. 很经典的一个期望dp的题,必然事件是在第n-1个格子期望抛掷一次就结束游戏,所以从这个已知的点来推为止的期望,也就成了逆推期望。在可以飞行的点,前面的点的期望值就是其飞行到达点的期望值,然后顺其自然的就可以状态转移了。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
int to[maxn],n,m;
double dp[maxn];
void init()
{
memset(dp,0,sizeof(dp));
memset(to,-1,sizeof(to));
while(m--)
{
int s,e;
cin>>s>>e;
to[s] = e;//记录飞行的到达的点
}
} void solve()
{
for(int i=n-1;i>=0;i--)
{
if(to[i] != -1)
dp[i] = dp[to[i]];
else
{
for(int k=i+1;k<=i+6;k++)//抛出大于个格子数的点也是合法的,所以骰子每一面的可能性一直都是一样的
dp[i] += dp[k]/6.0;
dp[i] += 1.0;
}
}
printf("%.4f\n",dp[0]);
} int main()
{
while(cin>>n>>m && n+m)
{
init();
solve();
}
return 0;
}

HUD:4405-Aeroplane chess(期望飞行棋)的更多相关文章

  1. HDU 4405 Aeroplane chess 期望dp

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

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

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

  3. HDU 4405 Aeroplane chess:期望dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4405 题意: 你在下简化版飞行棋... 棋盘为一个线段,长度为n. 上面有m对传送门,可以直接将你从a ...

  4. HDU 4405 Aeroplane chess (概率DP求期望)

    题意:有一个n个点的飞行棋,问从0点掷骰子(1~6)走到n点须要步数的期望 当中有m个跳跃a,b表示走到a点能够直接跳到b点. dp[ i ]表示从i点走到n点的期望,在正常情况下i点能够到走到i+1 ...

  5. 【刷题】HDU 4405 Aeroplane chess

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

  6. hdu 4405 Aeroplane chess (概率DP)

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

  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 [期望DP]

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

  9. HDU4405 Aeroplane chess(期望dp)

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

随机推荐

  1. Win10 插入耳机后没有声音,拔出后电脑有声音

  2. input文本框默认提示

    今天闲暇时间把自己以前写的一个文本框默认提示函数改成了一个小插件.下面是代码 1.引入jQuery库 <script src="http://code.jquery.com/jquer ...

  3. 程序运行过程中遇到“ORA-03114: not connected to ORACLE”的问题解决

    c#,winform程序,数据批量入oracle库时用到DataAdaper的.FillSchema函数,如:da.FillSchema(dt2, SchemaType.Mapped); 程序运行一段 ...

  4. springMVC数据校验与单文件上传

    spring表单标签:    <fr:from/> 渲染表单元素    <fr:input/>输入框组件    <fr:password/>密码框组件标签    & ...

  5. Redis数据库1

    一.启动服务 #进入redis安装文件夹 cd /usr/local/redis/ #开启服务端(后端开启) ./bin/redis-server ./redis.conf #开启客户端 ./bin/ ...

  6. EasyUI:EasyUI-DataGrid多行合并实现

    1.首先我们<table>增加一个属性data-options="onLoadSuccess:mergeCells",mergeCells是我们对应的JS方法名,意思就 ...

  7. LoadRunner使用(2)

    一.基础函数 在VU左边导航栏中,有三个LoadRunner框架函数,分别是Vuser_init(),Action(),vuser_end().这三个函数存在于任何Vuser类型的脚本中. vuser ...

  8. Java的三大特性之继承

    此处我会分为这几个部分来理解继承是怎么样的: 1.区分封装.继承和多态 2.区分限定词的范围 3.区分隐藏.覆盖.重载 4.继承的理解 5.一道面试题的原型 --------------------- ...

  9. Ajax经典的面试题

    1.什么是AJAX,为什么要使用Ajax(请谈一下你对Ajax的认识)什么是ajax:AJAX是“Asynchronous JavaScript and XML”的缩写.他是指一种创建交互式网页应用的 ...

  10. 使用JS的画布制作一个瞄准镜

    <canvas width="600" height="500" id="myCanvas"></canvas> & ...