【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 ...
随机推荐
- netty学习记录2
昨天晚上在看到7.2章MessagePack编码器和解码器开发这一章时,书里面没有贴出全部的代码,然后我按照我自己的想法把代码补全后,发现死活没有把代码跑通. 然后花了挺多时间在网上找,很多博客都贴出 ...
- 孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块
孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块 (完整学习过程屏幕记录视频地址在文末) 从今天起开始正式学习Python的爬虫. 今天已经初步了解了两个主要的模块: ...
- 问题 A: 分数矩阵
题目描述 我们定义如下矩阵:1/1 1/2 1/31/2 1/1 1/21/3 1/2 1/1矩阵对角线上的元素始终是1/1,对角线两边分数的分母逐个递增.请求出这个矩阵的总和. 输入 输入包含多组测 ...
- ubuntu安装显卡驱动和cuda
NVIDIA-linux.run安装后,会出现登录页面循环,解决办法是在运行命令后加入-no-opengl-files 打开nvidia x server Settings软件,显示:You do n ...
- web3无法安装的额解决方案-----yarn命令安装web3
凡是可以用 JavaScript 来写的应用,最终都会用 JavaScript 来写. --Atwood定律(Jeff Atwood在2007年提出) yarn命令详解 https://yarnpkg ...
- poj1094拓扑排序
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29539 Accepted: 10 ...
- 关于<!DOCTYPE html>的学习(转)
DOCTYPE是对Document type的缩写,说明用XHTML或者HTML是什么版本的.必须出现在<html>标签的前面,不需要关闭标签. <!DOCTYPE>声明不是标 ...
- Winform常用知识总结
Label中的文字自动换行 设置MaximumSize的width为正确的值,设置height为0,设置AutoSize为true. 绘制线条 放置一个Panel,设置size的高度为1,设置Bord ...
- [C/C++] 智能指针学习
转自:http://blog.csdn.net/xt_xiaotian/article/details/5714477 一.简介 由于 C++ 语言没有自动内存回收机制,程序员每次 new 出来的内存 ...
- JAVA高并发处理------多线程
线程安全概念:当多个线程访问某一个类(对象或方法)时,这个对象始终都能表现出正确的行为,那么这个类(对象或方法)就是线程安全的. 分析:当多个线程访问myThread的run方法时,以排队的方式进行处 ...