Aeroplane chess

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

Total Submission(s): 1503    Accepted Submission(s): 1025

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

学习概率DP推荐一个链接:http://kicd.blog.163.com/blog/static/126961911200910168335852/

思路:由当前点能够走向以下6个相邻位置,走到这几个点的概率均相等。用dp[i]表示该点走到目标的期望步数,则该点的期望能够由它能够到达的6个点相加得到,由于它走到下一个位置花费时间1,故要加一。见式子:

dp[0]=dp[1]*1/6+dp[2]*1/6+dp[3]*1/6+dp[4]*1/6+dp[5]*1/6+dp[6]*1/6+1;
dp[n]=0(自身到自身期望为0)

那么,我们倒着推过来就能得到答案为dp[0]。

#include"stdio.h"
#include"string.h"
#include"iostream"
#include"algorithm"
#include"math.h"
#include"vector"
using namespace std;
#define LL __int64
#define N 100005
#define max(a,b) (a>b? a:b)
vector<int>g[N];
int vis[N];
double dp[N];
int main()
{
int n,m,i,j,v,a,b;
while(scanf("%d%d",&n,&m),n||m)
{
for(i=0;i<=n;i++)
g[i].clear();
for(i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
g[b].push_back(a);
}
memset(dp,0,sizeof(dp)); //易知dp[n]=0
memset(vis,0,sizeof(vis));
for(i=0;i<g[n].size();i++)
{
v=g[n][i];
dp[v]=dp[n];
vis[v]=1;
}
for(i=n-1;i>=0;i--)
{
if(!vis[i])
{
for(j=i+1;j<=i+6;j++)
{
dp[i]+=dp[j]/6;
}
dp[i]+=1;
}
for(j=0;j<g[i].size();j++)
{
v=g[i][j];
dp[v]=dp[i];
vis[v]=1;
}
}
printf("%.4f\n",dp[0]);
}
return 0;
}

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

  1. [ACM] hdu 4405 Aeroplane chess (概率DP)

    Aeroplane chess Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 ...

  2. HDU 4405 Aeroplane chess (概率DP)

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

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

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

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

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

  5. HDU 4405 Aeroplane chess 期望dp

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

  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

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

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

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

随机推荐

  1. CentOS7 安装chrome浏览器

    本篇文章主要记录如何在CentOS7.0上安装chrome. 1.配置yum下载源: 在目录 /etc/yum.repos.d/ 下新建文件 google-chrome.repo, 并且在该文件中添加 ...

  2. js字符串转json

    1,eval方式解析,这是最早的解析方式了.如下: 代码如下: function strToJson(str){ var json = eval('(' + str + ')'); return js ...

  3. 3、第3课CSS块级、行内元素、绝对定位、相对定位、固定位置20150922

    1.块级元素 A:特点: A.1默认显示在父标签的左上角 A.2块级元素默认占满一行(占满整个文档流) B:常见的块级元素 P   h1--h6   ul  li    ol  li   div  h ...

  4. 配置 VS 2015 开发跨平台手机应用

    为了使用 VS 2015 开发跨平台手机应用,VS 2015 装了很多次,遇到了很多坑,才终于弄明白怎样配置才能正常使用C#开发手机应用,现把步骤分享给大家,以免大家少走弯路. 运行环境: Windo ...

  5. Visual Studio 2015 RC Downloads

    https://www.visualstudio.com/en-us/downloads/visual-studio-2015-downloads-vs

  6. [目录][总结] C++和Java 中的主要操作对比

    总结一些,C++ 和Java 中的一些常用操作对比,就当是自己的查询工具书啦.(暂时按随笔的更新时间排序) [Stack] c++ V.S. Java (2015.04.27) [Map]   c++ ...

  7. mybatis入门,基本案例和xml讲解

    mybatis入门 先举一个入门案例 1)创建一个mybatis-day01这么一个javaweb工程或java工程 2)导入mybatis和mysql/oracle的jar包到/WEB-INF/li ...

  8. html Table实现表头固定

    最近一直在搞前台琐碎的东西,也学习了一下linux,没有时间对新的东西进行深入的研究和学习,没有写博客,不过归咎其原因还是在于自己的惰怠. 废话不多说,今天想将一个前台页面设计的一个小东西分享一下,那 ...

  9. JS 操作一个数据值

    任何语言都有自己的操作数据的方法: Js也不例外,js有3种重要的方式来操作一个数据值. 1>复制它.例如把它赋给一个新的变量. 2>把它作为参数传递给一个函数或方法. 3>可以和其 ...

  10. Tr A(HDU 1575 快速矩阵幂模板)

    Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...