Aeroplane chess

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1122    Accepted Submission(s): 762

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
 

/*题意:有一个飞行棋n个格子,刚開始在0这个位置,每次能够扔色子,扔到x则能够移动x格
假设到达的位置>=n则胜利
另外在棋盘上还有m对点x,y表示在位置x能够直接飞行到y
求到达胜利平均的扔色子次数 分析:求期望,假设dp[i]表示在点i位置到达胜利所须要的平均次数,则我们须要求dp[0]
dp[n],dp[n+1]....等是知道的:为0
而对于dp[i]能够到达:
假设点i不能飞行:dp[i+1],dp[i+2],dp[i+3],dp[i+4],dp[i+5],dp[i+6]且等概率:1/6
假设i点位置能够飞行则能够到达飞行的点
由E(aA+bB+cC+dD...)=aEA+bEB+....
可知:dp[i]=1/6*dp[i+1]+1/6*dp[i+2]...
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef long long LL;
using namespace std; const int MAX=100000+10;
int n,m,size;
int head[MAX];
double dp[MAX];//dp[i]表示从i到达目标所须要的平均次数(期望) struct Node{
int y,next;
Node(){}
Node(int Y,int NEXT):y(Y),next(NEXT){}
}node[MAX]; void Init(){
memset(head,-1,sizeof head);
memset(dp,0,sizeof dp);
size=0;
} void InsertNode(int x,int y){
node[size]=Node(y,head[x]);
head[x]=size++;
} double Solve(int x){
double sum=0,num=0;
for(int i=head[x];i != -1;i=node[i].next){
sum+=dp[node[i].y];
++num;
}
return sum/num;
} int main(){
int x,y;
while(~scanf("%d%d",&n,&m),n+m){
Init();
for(int i=0;i<m;++i){
scanf("%d%d",&x,&y);
InsertNode(x,y);
}
for(int i=n-1;i>=0;--i){
if(head[i] != -1){
dp[i]=Solve(i);
}else{
dp[i]=(dp[i+1]+dp[i+2]+dp[i+3]+dp[i+4]+dp[i+5]+dp[i+6])/6+1;
}
}
printf("%.4lf\n",dp[0]);
}
return 0;
}

hdu4405概率dp入门的更多相关文章

  1. HDU 3853 LOOPS 概率DP入门

    LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)Total Sub ...

  2. 概率dp入门

    概率DP主要用于求解期望.概率等题目. 转移方程有时候比较灵活. 一般求概率是正推,求期望是逆推.通过题目可以体会到这点. poj2096:Collecting Bugs #include <i ...

  3. 概率DP入门学习QAQ

    emmmm博客很多都烂尾了...但是没空写..先写一下正在学的东西好了 概率DP这东西每次考到都不会..听题解也是一脸懵逼..所以决定学习一下这个东东..毕竟NOIP考过...比什么平衡树实在多了QA ...

  4. HDU 4405:Aeroplane chess(概率DP入门)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Problem Description   Hzz loves ...

  5. poj 2096 Collecting Bugs 概率dp 入门经典 难度:1

    Collecting Bugs Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 2745   Accepted: 1345 ...

  6. 洛谷P2719 搞笑世界杯 题解 概率DP入门

    作者:zifeiy 标签:概率DP 题目链接:https://www.luogu.org/problem/P2719 我们设 f[n][m] 用于表示还剩下n张A类票m张B类票时最后两张票相同的概率, ...

  7. POJ 2096-Collecting Bugs(概率dp入门)

    题意: 有n种bug和s种系统bug,每天发现一种bug(可能已经发现过了)所有种bug被发现的概率相同,求所有bug被发现的期望天数. 分析: dp[i][j]发现i种bug,j种系统bug期望天数 ...

  8. HDU 3853-loop(概率dp入门)

    题意: r*c个方格,从(1,1)开始在每个方格可释放魔法(消耗能量2)以知,释放魔法后可能在原地.可能到达相邻的下面格子或右面格子,给出三者的概率 求要到达(R,C)格子,要消耗能量的期望值. 分析 ...

  9. hdu3853之概率dp入门

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xingyeyongheng/article/details/25205693 LOOPS Time ...

随机推荐

  1. 深入解读JavaScript面向对象编程实践

    面向对象编程是用抽象方式创建基于现实世界模型的一种编程模式,主要包括模块化.多态.和封装几种技术.对JavaScript而言,其核心是支持面向对象的,同时它也提供了强大灵活的基于原型的面向对象编程能力 ...

  2. 200常用JS

    .文本框焦点问题 onBlur:当失去输入焦点后产生该事件 onFocus:当输入获得焦点后,产生该文件 Onchange:当文字值改变时,产生该事件 Onselect:当文字加亮后,产生该文件 &l ...

  3. Maven POM配置释疑

    1.  对于有父子关系的Project, 父POM中依赖使用dependencies 和 dependencyManagement 的区别: dependencies: 即使子项目中不写该依赖项,仍然 ...

  4. Oracle "Job定时"

    今天需要做个定时器,定时到别的库导入数据用到了Job,第一次使用记录下来,如果有第一次操作的可以借鉴一下 1.首先,使用Toad新建job,进入配置页面

  5. [LeetCode]题解(python):097-Interleaving String

    题目来源: https://leetcode.com/problems/interleaving-string/ 题意分析: 给定字符串s1,s2,s3,判断s3是否由s1和s2穿插组成.如“abc” ...

  6. https tomcat 证书搭建

    首先生成证书说明 keytool -genkey -alias castest -keyalg RSA -keystore c:/keys/caskey 先让输入密码,密码必须记住,下面会用到 其中“ ...

  7. selenium 学习笔记 ---新手学习记录(5) 问题总结(java)

    1.今天遇到个奇葩问题,iframe有两个id相同的(如下图) 使用driver.switchTo().frame(“frmLinkPage1”);这个无法使用了. 后来改用driver.switch ...

  8. ASP.Net MVC3 - The easier to run Unit Tests by moq #Reprinted#

    From: http://www.cnblogs.com/techborther/archive/2012/01/10/2317998.html 前几天调查完了unity.现在给我的任务是让我调查Mo ...

  9. 我的Fedora环境

    Fedora现在也更新到了第20个版本,只是在15+以后的版本,大多数操作,都是大同小异的,也不必特意去关注版本号,只有对应到具体的软件,可能会因为库的版本,有或多或少的区别. 之前每次都喜欢按照一些 ...

  10. 高频(工作频率为13.56MHz)

    在该频率的感应器不再需要线圈进行绕制,可以通过腐蚀活着印刷的方式制作天线.感应器一般通过负载调制的方式 的方式进行工作.也就是通过感应器上的负载电阻的接通和断开促使读写器天线上的电压发生变化,实现用远 ...