Aeroplane chess

Time Limit: 1 Sec  Memory Limit: 32 MB
[Submit][Stataus][Discuss]

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.
  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

HINT

  1≤N≤100000, 0≤M≤1000

Main idea

  从0走到n-1,每次以均等概率走1~6步,某些点可以直接跨越到指定点,求走出n所需走的次数的期望。

Solution

  我们直接使用期望DP求解。令 f[i] 表示到位置 i 的期望,然后直接从后往前递推即可。

Code

 #include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<bitset>
using namespace std;
const int ONE = ; int n,m;
int x,y,To[ONE];
double f[ONE]; int get()
{
int res=,Q=; char c;
while( (c=getchar())< || c>)
if(c=='-')Q=-;
if(Q) res=c-;
while((c=getchar())>= && c<=)
res=res*+c-;
return res*Q;
} void Solve()
{
n=get(); m=get();
if(!n && !m) exit();
memset(To,,sizeof(To)); memset(f,,sizeof(f));
for(int i=;i<=m;i++)
{
x=get(); y=get();
To[x] = y;
} for(int i=n-;i>=;i--)
{
if(To[i]) {f[i] = f[To[i]]; continue;}
for(int j=;j<=;j++)
f[i] += (double)/ * f[min(i+j,n)];
f[i]++;
} printf("%.4lf\n",f[]);
} int main()
{
for(;;)
Solve();
}

【HDU4405】Aeroplane chess [期望DP]的更多相关文章

  1. HDU4405 Aeroplane chess(期望dp)

    题意 抄袭自https://www.cnblogs.com/Paul-Guderian/p/7624039.html 正在玩飞行棋.输入n,m表示飞行棋有n个格子,有m个飞行点,然后输入m对u,v表示 ...

  2. HDU 4405 Aeroplane chess 期望dp

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

  3. [hdu4405]Aeroplane chess(概率dp)

    题意:某人掷骰子,数轴上前进相应的步数,会有瞬移的情况,求从0到N所需要的期望投掷次数. 解题关键:期望dp的套路解法,一个状态可以转化为6个状态,则该状态的期望,可以由6个状态转化而来.再加上两个状 ...

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

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

  5. 2018.09.01 hdu4405 Aeroplane chess (期望dp)

    传送门 期望dp简单题啊. 不过感觉题意不太对. 手过了一遍样例发现如果有捷径必须走. 这样的话就简单了啊. 设f[i]" role="presentation" sty ...

  6. HDU4405 Aeroplane chess 飞行棋 期望dp 简单

    http://acm.hdu.edu.cn/showproblem.php?pid=4405   题意:问从起点到终点需要步数的期望,1/6的概率走1.2.3.4.5.6步.有的点a有路可以直接到b, ...

  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

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

  9. HDU-4405 Aeroplane chess

    http://acm.hdu.edu.cn/showproblem.php?pid=4405 看了一下这个博客http://kicd.blog.163.com/blog/static/12696191 ...

随机推荐

  1. OrCAD把原理图中的器件添加到原理图库

    1. 在使用OrCAD的时候,有时需要把别人的原理图里面的器件添加到自己的原理图库,方便以后使用,具体操作如下,依次选择Design Cache---元器件--Copy 2. 选中要存放的原理图库,鼠 ...

  2. 玩转Vim-札记(二)

    玩转Vim-札记(二) 距上篇博文已有一周有余,上次主要介绍了编辑器之神Vim的起源.安装并介绍了两种模式以及一些简单的操作.本次将继续对Vim的使用进行介绍. 登堂入室 首先接着说移动吧: 0 → ...

  3. Ubuntu下使用Git_4

    在这个第四个文章中,我将练习GIT的高级阶段了,由于高级阶段的内容转的比较多,我自己的代码除了我自己可以看懂意外,大家可能看不懂,所以我将会按照 http://git.wiki.navisec.it/ ...

  4. Linux-ls,cd,type命令

    windows: dll:dynamic link library,动态链接库 Linux: .so:shared object,共享对象 操作系统: kernel:内核: 1.进程管理 2.内核管理 ...

  5. QC的使用学习(二)

    今日学习清单: 1.Quality  Center中左上角选项中(QC 10.0中文版)工具菜单下的自定义中的几个内容,有:用户属性.组.项目用户.模块访问.需求类型.项目列表等.用户属性打开后是对当 ...

  6. Spring实战第七章————SpringMVC配置的替代方案

    SpringMVC配置的替代方案 自定义DispatherServlet配置 我们之前在SpittrWebAppInitializer所编写的三个方法仅仅是必须要重载的abstract方法.但还有更多 ...

  7. gcc options选项的优化及选择

    gcc options选项的优化 -c和-o都是gcc编译器的可选参数[options] -c表示只编译(compile)源文件但不链接,会把.c或.cc的c源程序编译成目标文件,一般是.o文件.[只 ...

  8. LeetCode 29——两数相除

    1. 题目 2. 解答 2.1. 方法一 题目要求不能使用乘法.除法和除余运算,但我们可以将除法转移到对数域. \[ \frac{a}{b} = e^{\frac{lna}{lnb}} = e^{ln ...

  9. c# 把List<T>转成DataTable对象,批量导入Sqlserver库

    /// <summary> /// Sqlbulkcopies the specified SMS.批量插入到数据库 /// </summary> /// <param ...

  10. Spring MVC自动为对象注入枚举数据

    一.实现转换工厂,定义转换实现,如下:     package com.mafwo; import org.springframework.core.convert.converter.Convert ...