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. windows安装Apache,注册服务出现“(OS 5)拒绝访问。 : AH00369: Failed to open the WinNT service manager..."错误

    原文:http://blog.csdn.net/jaray/article/details/9950211 在安装Apache的时候,我下载的是zip格式,不是msi安装版,需要自己注册服务,才能在桌 ...

  2. 在开发 ExtJS 应用程序常犯的 10 个错误

    这是 CNX 公司在开发 ExtJS 项目中总结的需要特别注意的 10 个地方.有时候,我们完全是自己使用 ExtJS 从零开始构建的新的应用程序,但有时候我们的客户会要求我们使用他们自己的代码,并且 ...

  3. while和do while习题

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 练习 { ...

  4. 使用内容提供者和xml备份联系人

    1.通过内容提供者获取联系人信息 package com.ithaimazyh.readcontact; import java.util.ArrayList; import java.util.Li ...

  5. [Android] 更改关联的源码路径

    右击选中工程 → Java Build Path → Libraries → Android 4.1.2 → 点开android.jar → 选中Source attachment → Edit,即可 ...

  6. SQL SERVER 2008- 字符串函数

    /* 1,ASCII返回字符表达式中最左侧字符的ASCII代码值 仅返回首字母的ASCII码值 parameter char或varchar returns integer */ SELECT ASC ...

  7. Qt之开机自启动及拥有管理员权限

    源地址:http://blog.sina.cn/dpool/blog/s/blog_a6fb6cc90101feia.html Windows开机自启动的程序很多,包括系统软件.杀毒软件.一些其他安装 ...

  8. 用Delphi实现Windows的鼠标钩子函数

    Delphi是基于PASCAL语言的Windows编程工具,功能十分强大.然而在Delphi的帮助文件中,对Windows API函数的说明沿袭了 VC 的格式,和VC一样,对很多API函数的用法没有 ...

  9. css3 animation 参数详解

    animation: name 2s ease 0s 1 both有人知道这后面的参数都代表什么意思吗 name 就是你创建动画的名称 2S表示的时长 ease表示运动效果 0S表示延迟时间 1表示的 ...

  10. Java 实现观察者(Observer)模式

    1. Java自带的实现 类图 /** * 观察目标 继承自 java.util.Observable * @author stone * */ public class UpdateObservab ...