【HDU4405】Aeroplane chess [期望DP]
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
8 3
2 4
4 5
7 8
0 0
Sample Output
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]的更多相关文章
- HDU4405 Aeroplane chess(期望dp)
题意 抄袭自https://www.cnblogs.com/Paul-Guderian/p/7624039.html 正在玩飞行棋.输入n,m表示飞行棋有n个格子,有m个飞行点,然后输入m对u,v表示 ...
- HDU 4405 Aeroplane chess 期望dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Time Limit: 2000/1000 MS (Java/ ...
- [hdu4405]Aeroplane chess(概率dp)
题意:某人掷骰子,数轴上前进相应的步数,会有瞬移的情况,求从0到N所需要的期望投掷次数. 解题关键:期望dp的套路解法,一个状态可以转化为6个状态,则该状态的期望,可以由6个状态转化而来.再加上两个状 ...
- HDU4405 Aeroplane chess (概率DP,转移)
http://acm.hdu.edu.cn/showproblem.php?pid=4405 题意:在一个1×n的格子上掷色子,从0点出发,掷了多少前进几步,同时有些格点直接相连,即若a,b相连,当落 ...
- 2018.09.01 hdu4405 Aeroplane chess (期望dp)
传送门 期望dp简单题啊. 不过感觉题意不太对. 手过了一遍样例发现如果有捷径必须走. 这样的话就简单了啊. 设f[i]" role="presentation" sty ...
- HDU4405 Aeroplane chess 飞行棋 期望dp 简单
http://acm.hdu.edu.cn/showproblem.php?pid=4405 题意:问从起点到终点需要步数的期望,1/6的概率走1.2.3.4.5.6步.有的点a有路可以直接到b, ...
- [ACM] hdu 4405 Aeroplane chess (概率DP)
Aeroplane chess Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 ...
- hdu4405 Aeroplane chess
Aeroplane chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU-4405 Aeroplane chess
http://acm.hdu.edu.cn/showproblem.php?pid=4405 看了一下这个博客http://kicd.blog.163.com/blog/static/12696191 ...
随机推荐
- PHP批量替换MySql数据库中的数据内容
<?php //替换数据库内容类 class replace{ public $dbAddress; //数据库地址 public $dbUser; //数据库用户名 public $dbPwd ...
- PowerMock简单使用
网上有很多PowerMock的介绍,此处就不再罗列 下面给出一些资源地址以及几篇案例 mockito资源: (1)源码:https://github.com/mockito/mockito power ...
- iOS-合成图片(长图)
合成图片 直接合成图片还是比较简单的,现在的难点是要把,通过文本输入的一些基本数据也合成到一张图片中,如果有多长图片就合成长图. 现在的实现方法是,把所有的文本消息格式化,然后绘制到一个UILable ...
- request.getparameter() 获取中文出现乱码 问题
http请求是以ISO-8859-1的编码来传送url的 如果页面的content-type为utf-8,那么在发送请求时,会将字符转成utf-8后进行传送 如: 中 的UTF-8编码为:E4 B8 ...
- 步骤1:JMeter 录制脚本接口测试
JMeter 常用测试方法简介 1.下载安装 http://jmeter.apache.org/download_jmeter.cgi 安装JDK,配置环境变量JAVA_HOME. 系统要求:JMet ...
- jdk8 新特性stream().map()
1.大写字符串列表 1.1 简单的Java示例将Strings列表转换为大写 TestJava8.java package com.mkyong.java8; import java.util.Arr ...
- Qt 贪吃蛇小游戏
简单的实现了走和变大的样子,剩下的还在完善 贴代码 #include "mainwindow.h" #include "ui_mainwindow.h" #in ...
- Appium iOS万能的定位方式--Predicate(iOSNsPredicate)
所谓Predicate定位即Java-Client -5.0.版本以及Appium-Python-Client 0.31版本更新后增加的新的定位方式: 举个例子: JAVA代码: //输入账号和密码 ...
- Hibernate配置实体类的属性
Hibernate配置实体类的属性既可以在页面显示关联实体类的所有属性,在插入该属性时又可以只插入单一属性 private String companyCode; private CompanyEnt ...
- 【Linux】Linux修改openfile和max user processes?
#当时测试虚机为centos7.4版本: # 在/etc/security/limits.conf文件末尾添加如下命令: * soft nproc 1314 * hard ...