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. C#面向对象2 静态类、静态成员的理解

    理解:静态成员属于类所有,为各个类的实例所公用,与实例无关,需要全局共享的属性或者方法定义成静态的 C#静态成员:  1.静态成员属于类所有,故用类名调用,非静态成员属于类的实例所有,用实例名调用  ...

  2. springMVC 使用jstl

    jsp页面获取数据,感觉最方便的就是使用jstl+EL了,各种封装好的函数非常简单易用,接下来写如何使用jstl: 1.下载jstl-1.2_1.jar 2.由于项目是: xmlns="ht ...

  3. 一起C语言中程序时序问题的排查过程

    [文章摘要] 对于由多个模块协同工作的软件来说,程序处理的时序是很重要的.当消息处理的顺序出现混乱时,程序就会出现异常. 本文基于作者的实际项目经验.对软件模块之间的时序问题进行了具体的分析,为相关软 ...

  4. Codeforces Round #254 (Div. 2)D(预计)

    D. DZY Loves FFT time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  5. HDU4850 构造一个长度n串,它需要随机长度4子是不相同

    n<=50W.(使用26快报) 构造函数:26一个.截至构建26^4不同的字符串,最长的长度26^4+3.如此之大的输出"impossble",被判重量的四维阵列. 在正向结 ...

  6. Oracle基础知识笔记(10) 约束

    表尽管建立完毕了,可是表中的数据是否合法并不能有所检查,而假设要想针对于表中的数据做一些过滤的话,则能够通过约束完毕,约束的主要功能是保证表中的数据合法性,依照约束的分类,一共同拥有五种约束:非空约束 ...

  7. 关于android的nfc问题

    最近在研究android的nfc问题 首先再网上有很多关于android nfc 读写的问题,但是大多数都是关于Mifare Classic类型卡的读写,我百试不得骑解,于是自己写了一些程序供大家参考 ...

  8. Qt之开机自启动

    Windows开机自启动的程序很多,包括系统软件.杀毒软件.一些其他安装软件等等.当然可以禁止,通过软件管理或者手动删除对应的注册表中的项即可!但是为了系统的服务或者操作上的方便,我们往往需要开机自启 ...

  9. jquery mobile -role

    jquery mobile -role - cc_jony - 博客园 jquery mobile -role   data-page 页面 data-header 页面的头部 data-conten ...

  10. for循环遍历字符串的还有一种方法

    遍历字符c,让它各自等于字符串数组chars里面的各个字符.然后运行以下的语句,当c被赋值为chars里面全部字符各一次后.就会退出这个循环. 通常我们遍历字符串数组用 for(int i=0;i&l ...