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. 转 如何观察 undo Oracle DML语句回滚开销估算

    https://searchdatabase.techtarget.com.cn/7-20392/ --use_urec 详细解读: select USED_UREC from v$transacti ...

  2. code review的意义

    https://blog.csdn.net/brodycai/article/details/19636621

  3. P4874 回形遍历 —模拟

    思路: 写完后信心满满,结果超时. 我很不解,下了个数据结果——,z竟然是大于1e10的,跟题目给的不一样啊 原来如此,正解是一行一行的走的... 注意当到两边一样近时,应优先向下和右!!!!!! 这 ...

  4. babel7中 corejs 和 corejs2 的区别

    babel7中 corejs 和 corejs2 的区别 最近在给项目升级 webpack4 和 babel7,有一些改变但是变化不大.具体过程可以参考这篇文章 webpack4:连奏中的进化.只是文 ...

  5. Java基础(变量、运算符)

    第2天 Java基础语法 今日内容介绍 u 变量 u 运算符 第1章 变量 1.1 变量概述 前面我们已经学习了常量,接下来我们要学习变量.在Java中变量的应用比常量的应用要多很多.所以变量也是尤为 ...

  6. 【复习笔记】HTML基础

    编码 HTML LANG标注整体文档语言 常用编码:ASCII.GB2312.UTF-8 中文编码解决: 1.浏览器要用一个编码表去看你的文件<meta charset="utf-8& ...

  7. 织梦修改“dedecms提示信息”

    1.根目录下include文件夹,找到common.func.php: 2.根目录下dede文件夹(管理目录默认dede),找到sys_data_done.php: 3.打开以上2个.php文件,把“ ...

  8. i-nex安装教程

    sudo add-apt-repository ppa:i-nex-development-team/stable sudo apt-get updatesudo apt-get i-nex

  9. rhythmbox插件开发笔记3:安装 makefile && schema && po

    本篇主要讲通过makefile方式来安装rhythmbox插件的相关知识. makefile 如果makefile是什么,请自行谷歌 参考了pandasunny同学的rhythmbox-baidu-m ...

  10. UVA 1608 Non-boring sequence 不无聊的序列(分治,中途相遇)

    题意:给你一个长度为n序列,如果这个任意连续子序列的中都有至少出现一次的元素,那么就称这个序列是不无聊的,判断这个序列是不是无聊的. 先预处理出每个元素之前和之后相同元素出现的位置,就可以在O(1)的 ...