http://acm.split.hdu.edu.cn/showproblem.php?pid=4405

Aeroplane chess

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

概率DP主要用于求解期望、概率等题目。

一般求概率是正推,求期望是逆推。

kuangbin的概率DP学习网址:http://www.cnblogs.com/kuangbin/archive/2012/10/02/2710606.html

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define N 100010 double dp[N];
int nxt[N]; int main()
{
int n, m;
while(~scanf("%d%d", &n, &m), n+m) {
memset(nxt, -, sizeof(nxt));
for(int i = ; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
nxt[u] = v;
}
memset(dp, , sizeof(dp));
double dec = (double) / ;
for(int i = n - ; i >= ; i--) {
if(nxt[i] != -) {
dp[i] = dp[nxt[i]]; //如果可以飞,就直接把上一步的值赋给它
continue;
}
for(int j = ; j <= ; j++) {
if(i + j <= n) {
dp[i] += dp[i + j] * dec; //不能飞的话,就掷骰子为1-6的概率都为1/6,递推
}
}
dp[i]++; //走到下一步要+1
}
printf("%.4f\n", dp[]);
}
return ;
}

HDU 4405:Aeroplane chess(概率DP入门)的更多相关文章

  1. [ACM] hdu 4405 Aeroplane chess (概率DP)

    Aeroplane chess Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 ...

  2. HDU 4405 Aeroplane chess (概率DP)

    题意:你从0开始,要跳到 n 这个位置,如果当前位置是一个飞行点,那么可以跳过去,要不然就只能掷骰子,问你要掷的次数数学期望,到达或者超过n. 析:概率DP,dp[i] 表示从 i  这个位置到达 n ...

  3. HDU 4405 Aeroplane chess 概率DP 难度:0

    http://acm.hdu.edu.cn/showproblem.php?pid=4405 明显,有飞机的时候不需要考虑骰子,一定是乘飞机更优 设E[i]为分数为i时还需要走的步数期望,j为某个可能 ...

  4. HDU 4405 Aeroplane chess(概率dp,数学期望)

    题目 http://kicd.blog.163.com/blog/static/126961911200910168335852/ 根据里面的例子,就可以很简单的写出来了,虽然我现在还是不是很理解为什 ...

  5. HDU 4405 Aeroplane chess 期望dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Time Limit: 2000/1000 MS (Java/ ...

  6. hdu 4405 Aeroplane chess (概率DP)

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

  7. hdu 4405 Aeroplane chess(概率+dp)

    Problem Description Hzz loves aeroplane chess very much. The chess map contains N+ grids labeled to ...

  8. hdu 4405 Aeroplane chess(简单概率dp 求期望)

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  9. 【刷题】HDU 4405 Aeroplane chess

    Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled fr ...

  10. HDU 4405 Aeroplane chess (概率DP求期望)

    题意:有一个n个点的飞行棋,问从0点掷骰子(1~6)走到n点须要步数的期望 当中有m个跳跃a,b表示走到a点能够直接跳到b点. dp[ i ]表示从i点走到n点的期望,在正常情况下i点能够到走到i+1 ...

随机推荐

  1. iOS TPKeyboardAvoiding自动识别键盘的高度

                     #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicatio ...

  2. iOS -Swift 3.0 -String(字符串常规用法)

    // // ViewController.swift // Swift-String // // Created by luorende on 16/9/10. // Copyright © 2016 ...

  3. 获取git的最后一次提交的commit id

    git log --pretty=format:"%h" | head -1  | awk '{print $1}' 可以放到xcode  build setting  post ...

  4. c#异步调用

    首先来看一个简单的例子: 小明在烧水,等水烧开以后,将开水灌入热水瓶,然后开始整理家务 小文在烧水,在烧水的过程中整理家务,等水烧开以后,放下手中的家务活,将开水灌入热水瓶,然后继续整理家务 这也是日 ...

  5. resin 64 & Java install

    [root@vohst voh]# uname -rnvohst 3.10.0-123.el7.x86_64 resin-pro-4.0.41.tar.gz Unrecognized option: ...

  6. JDK里的设计模式

    一.Creational patterns 创建模式 Abstract factory (recognizeable by creational methods returning the facto ...

  7. nodejs发起HTTPS请求并获取数据

    摘要:在网站中有时候需要跨域请求数据,直接用Ajax无法实现跨域,采用其他方式需要根据不同的浏览器做相应的处理.用Nodejs可以很好的解决这些问题,后台引用HTTPS模块,发送和返回的数据均为JSO ...

  8. PostgreSQL Replication之第十二章 与Postgres-XC一起工作(3)

    12.3 配置一个简单的集群 在本章中,我们要建立一个由三个数据节点组成的集群.一个协调节点,以及管理集群的全局事务管理节点.对于每个组件,我们必须创建一个目录: hs@vm:~/data$ ls - ...

  9. 【Origin】 破阵子-未可留 征人调

    几朝岁月,悠悠,容颜改,两鬓衰,可恨荣光不留! 一生事,忧心畔,可怜惶惶,不拿年岁当缠头: 只把扑朔往往,人生几回首: 等闲识得料峭处,一腔泪流: 曾记否,三十功名尘与土,不可解忧愁: 青春换得明日花 ...

  10. hdu5322 Hope

    设dp[n]为n个数字排列时候的答案,那么可以得到dp方程 dp[n]=Σdp[n-i]*c(n-1,i-1)*(i-1)!*i^2(1<=i<=n) 然后上式可以化成卷积形式,分治FFT ...