博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU4405--Aeroplane chess(概率dp)
阅读量:5290 次
发布时间:2019-06-14

本文共 2529 字,大约阅读时间需要 8 分钟。

题目

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

大意

有1~n,n个点,你从0号点开始跳,每次投骰子,点数是几你就向前走几步,然后有一些连接点x和点y的通道,这样你走到点x时会被直接传送到点y(如果点y又和点z间有个通道,你就会继续被传送到z,通道是单向的而且是从小的点传送到大的点(不会给你传回去) 求走到点n或比点n大的点就会停止,求停止时平均的投骰子次数

算是见过的比较简单的概率dp了,设dp[i]表示已经走到点i时到点n的期望投骰子数,

则如果i和某个点a之间有通道时

dp[i]=dp[a]dp[i]=dp[a]

否则的话

dp[i]=dp[i+1]/6+dp[i+2]/6+dp[i+3]/6+dp[i+4]/6+dp[i+5]/6+dp[i+6]/6+1.0dp[i]=dp[i+1]/6+dp[i+2]/6+dp[i+3]/6+dp[i+4]/6+dp[i+5]/6+dp[i+6]/6+1.0

很简单的递推公式了

然后会有多组数据,这算是个槽点吧

代码

#include
using namespace std;inline int read(){ int res=0; char ch=getchar(); while(!isdigit(ch)) ch=getchar(); while(isdigit(ch)) res=(res<<1)+(res<<3)+(ch^48),ch=getchar(); return res;}double dp[100010];int n,m,to[100005],x,y;int main(){ n=read(),m=read(); while(n!=0||m!=0) { memset(dp,0,sizeof(dp)); memset(to,0,sizeof(to)); for(int i=1;i<=m;i++) { x=read(),y=read();to[x]=y; } dp[n]=0.0; for(int i=n-1;i>=0;i--){ if(to[i]) { dp[i]=dp[to[i]];continue; } else dp[i]=dp[i+1]/6+dp[i+2]/6+dp[i+3]/6+dp[i+4]/6+dp[i+5]/6+dp[i+6]/6+1.0; } printf("%.4lf",dp[0]); puts(""); n=read(),m=read(); }}

转载于:https://www.cnblogs.com/forever-/p/9736080.html

你可能感兴趣的文章
HDOJ 4749 Parade Show
查看>>
python 多线程并发threading & 任务队列Queue
查看>>
【黑马程序员】资深程序员的见解
查看>>
1_fbauto
查看>>
IO体系、集合体系、多线程、jdbc
查看>>
关于时间:UTC/GMT/xST/ xDT
查看>>
[51Nod1089] 最长回文子串 V2(Manacher算法)
查看>>
Asp.Net生命周期系列六
查看>>
php引用 =& 详解
查看>>
Codeforces 914D Bash and a Tough Math Puzzle (ZKW线段树)
查看>>
POJ 3009: Curling 2.0
查看>>
DLNA介绍(包含UPnP,2011/6/20 更新)
查看>>
ANGULARJS5从0开始(2) - 整合bootstrap和font-awesome
查看>>
Android 使用Parcelable序列化对象
查看>>
Python Web框架Django (零)
查看>>
Foxmail出现 错误信息:553 mailbox not found怎么解决
查看>>
spring_远程调用
查看>>
js 中基本数据类型和引用数据类型 ,,,, js中对象和函数的关系
查看>>
登录服务器,首先用到的5个命令
查看>>
多米诺骨牌
查看>>