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

本文共 2646 字,大约阅读时间需要 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[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.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/stargazer-cyk/p/10366512.html

你可能感兴趣的文章
【POJ2699】The Maximum Number of Strong Kings(网络流)
查看>>
spring boot配置跨域
查看>>
BZOJ 1996 合唱队(DP)
查看>>
进击吧!阶乘——大数乘法
查看>>
安卓学习资料推荐-25
查看>>
Mysql数据库备份和还原常用的命令
查看>>
关于退出当前页面在火狐的一些问题
查看>>
【项目实施】项目考核标准
查看>>
spring-aop AnnotationAwareAspectJAutoProxyCreator类
查看>>
经典入门_排序
查看>>
Redis Cluster高可用集群在线迁移操作记录【转】
查看>>
二、spring中装配bean
查看>>
VIM工具
查看>>
javascript闭包
查看>>
@Column标记持久化详细说明
查看>>
创建本地yum软件源,为本地Package安装Cloudera Manager、Cloudera Hadoop及Impala做准备...
查看>>
mysql8.0.13下载与安装图文教程
查看>>
站立会议08(冲刺2)
查看>>
url查询参数解析
查看>>
http://coolshell.cn/articles/10910.html
查看>>