Aeroplane chess

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

解题思路:

题意为一条直线上有N+1个格子,编号0-N,也是其位置,有一枚骰子。比方当前位置你在i,骰子执到了 y点(1<=y<=6),那么下一步你就到了i+y位置,格子里有几个特殊的格子。能够从x位置瞬间转移到y位置,不须要步数,假设转移以后的格子还是特殊的格子,那么继续转移,要求的是,最后到达的位置>=n,所须要的投骰子次数的期望。

我们用 dp[i]表示当前位置到达位置>=n所须要投掷骰子的平均次数。

那么非常明显有 dp[n]=0; 而我们要求的则是dp[0] (起点)

注意两点:

①不遇到特殊格子时,投掷一次筛子,由当前位置i到达位置 ,i+1 , i+2 , i+3 , i+4 , i+5 ,i+6 的概率是一样的。都是 1/6,那么 dp[i]=(dp[i+1] +dp[i+2]+....dp[i+6])/6 +1 (须要多投掷一次筛子。所以+1).

②遇到特殊格子时。当前位置的投掷骰子的平均次数等于转移到的位置的平均次数.  比方由x到y。  那么 dp[x]=dp[y]。

依据以上两点。就能够写出代码.

dp[n]是已知的。须要从后往前推。

代码:

#include <iostream>
#include <iomanip>
#include <string.h>
using namespace std;
const int maxn=100005;
double dp[maxn];
int hash[maxn];
int n,m; int main()
{
while(cin>>n>>m&&(n||m))
{
int x,y;
memset(hash,0,sizeof(hash));
for(int i=1;i<=m;i++)
{
cin>>x>>y;
hash[x]=y;//标记一下,x位置的格子是特殊格子
}
dp[n]=0;
for(int i=n-1;i>=0;i--)
{
if(hash[i])//特殊格子
dp[i]=dp[hash[i]];
else
{
double temp=0;
for(int j=1;j<=6&&i+j<=n;j++)
temp+=dp[i+j]/6;
dp[i]=temp+1;
}
}
cout<<setiosflags(ios::fixed)<<setprecision(4)<<dp[0]<<endl; }
return 0;
}

[ACM] hdu 4405 Aeroplane chess (概率DP)的更多相关文章

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

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

  2. HDU 4405 Aeroplane chess (概率DP)

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

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

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

  4. HDU 4405 Aeroplane chess 期望dp

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

  5. hdu 4405 Aeroplane chess (概率DP)

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

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

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

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

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

  8. HDU 4405 Aeroplane chess:期望dp

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

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

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

随机推荐

  1. 用Python做2048游戏 网易云课堂配套实验课。通过GUI来体验编程的乐趣。

    第1节 认识wxpython 第2节 画几个形状 第3节 再做个计算器 第4节 最后实现个2048游戏 实验1-认识wxpython 一.实验说明 1. 环境登录 无需密码自动登录,系统用户名shiy ...

  2. textarea内容有换行时存入数据库丢失问题的解决 (转载)

    http://blog.csdn.net/zhang_j_h/article/details/44563167 存入: function GetInputData(id, cmd) { var pos ...

  3. PHP - 遍历文件夹下的所有文件名

    /** * * 函数名:myreaddir($dir) * 作用:读取目录所有的文件名 * 参数:$dir 目录地址 * 返回值:文件名数组 * * */ function myreaddir($di ...

  4. UML看书笔记1:主体思想

    对象设计需要解决的三大问题:      1.把“现实世界”映射到“对象世界”      2.从“对象世界”描述“现实世界”      3.验证“对象世界”是否反映了“现实世界” 现实世界与我们的对象世 ...

  5. 【jQuery】smartMenu右键自定义上下文菜单插件(似web QQ)

    (前端用重点整理博客地址)链接地址:http://www.cnblogs.com/atree/archive/2011/06/30/jQuery-smartMenu-javascript.html 一 ...

  6. js注册检测 用户名、密码、手机号、邮箱

    请输入电话号码:<input name="" type="text" id="telphone" value="" ...

  7. 基于visual Studio2013解决面试题之1109全排列

     题目

  8. Android Gradle Plugin指南(六)——高级构建定制

    原文地址:http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Advanced-Build-Customization ...

  9. C#、ASP.NET、WinForm - 实现邮件发送的功能

    转载自:http://www.cnblogs.com/mingmingruyuedlut/archive/2011/10/14/2212255.html 发送邮件所用的核心知识点 微软封装好的Mail ...

  10. Visual Studio2013创建、公布监控Windows Azure网站

    原文 Visual Studio2013创建.公布监控Windows Azure网站 随着Visual Studio 2013的发布,现在我们可以在Visual Studio内部实现Windows A ...