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. iOS面试题02-数据存储

    1.如果后期需要增加数据库中的字段怎么实现,如果不使用CoreData呢? 回答:编写SQL语句来操作原来表中的字段 1>增加表字段 ALETER TABLE 表名 ADD COLUMN 字段名 ...

  2. SQL Server2012新特性概述

    公司最近要升级数据库,SQL Server 2008R2-->2012.再开始升级之前先找了点资料分析一下2012的新特性和功能,提前预热一下. 2012中主要关注一下三个领域: 性能:改进的核 ...

  3. XML方式实现Spring声明式事务管理

    1.首先编写一个实体类 public class Dept { private int deptId; private String deptName; public int getDeptId() ...

  4. C3p0实践

    jar包 c3p0-0.9.2.1.jar mchange-commons-java-0.2.3.4.jar mysql-connector-java-5.1.28-bin.jar 建立数据库 CRE ...

  5. textwrap——文本包裹和填充模块解析

    textwrap模块提供了两个函数wrap()和fill(),以及TextWrapper类,以及另外一个工具函数dedent().         wrap()以及fill()都可以用来格式化一大段文 ...

  6. C++获取当前机器内网IP地址

    /*头文件*/ #include "winsock2.h" #pragma comment(lib,"ws2_32.lib") /*Hui 获取当前服务器IP* ...

  7. git版本控制的笔记

    一.配置你的身份,提交代码时git就可以知道是谁提交的了 git config --global user.name "Tony" git config --global user ...

  8. JDK JRE先保存 后面再整理

    1. 定义 JRE(Java Runtime Enviroment)是Java的运行环境.面向Java程序的使用者,而不是开发者.如果你仅下载并安装了JRE,那么你的系统只能运行Java程 序.JRE ...

  9. [转]android Handler使用

    转 http://blog.csdn.net/new_abc/article/details/8184634 不过这个我看不懂 不知道为什么i的值可以接着增长... package com.examp ...

  10. BZOJ 3211 花神游历各国 (树状数组+并查集)

    题解:首先,单点修改求区间和可以用树状数组实现,因为开平方很耗时间,所以在这个方面可以优化,我们知道,开平方开几次之后数字就会等于1 ,所以,用数组记录下一个应该开的数,每次直接跳到下一个不是1的数字 ...