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. BCP及自增标识列

    10:58 2012-12-20 通过BCP命令导入导出数据 bcp "test.dbo.lxy133" out d:\lxy133.txt -SMSSQL$SQL08R2 -Us ...

  2. Vue.2.0.5-表单控件绑定

    基础用法 你可以用 v-model 指令在表单控件元素上创建双向数据绑定.它会根据控件类型自动选取正确的方法来更新元素.尽管有些神奇,但 v-model 本质上不过是语法糖,它负责监听用户的输入事件以 ...

  3. HTML canvas font 属性

    定义和用法 font 属性设置或返回画布上文本内容的当前字体属性. font 属性使用的语法与 CSS font 属性 相同. 默认值: 10px sans-serif JavaScript 语法: ...

  4. perl 学习杂项笔记

    ### 由于perl 语法属于很自由的那种, 建议出现错误的时候打开 -w 或者使用 -Mdiagnositics 试一下 ### 如何调试 perl程序 http://www.ibm.com/dev ...

  5. iOS的三种多线程技术NSThread/NSOperation/GCD

    1.iOS的三种多线程技术 1.NSThread 每个NSThread对象对应一个线程,量级较轻(真正的多线程) 2.以下两点是苹果专门开发的"并发"技术,使得程序员可以不再去关心 ...

  6. UIButton属性

    1.UIButton状态: UIControlStateNormal          // 正常状态    UIControlStateHighlighted     // 高亮状态    UICo ...

  7. jquery-mockjax初试

    1. 原理 jquery-mockjax是用于mock 前台ajax向后台请求的返回数据. 原理很简单 在你js代码要发送ajax请求的地方断点一下,然后比较在[引入jquery-mockjax] 和 ...

  8. git提交

    1.git pull 本地已经commit 2.git checkout master 3.git pull 4.git checkout - 5.git merge master 6.git pus ...

  9. Swift游戏实战-跑酷熊猫 01 创建工程导入素材

    在这节里,我们将建立一个游戏工程,并导入一些必要的素材,例如序列帧动画文件,声音素材文件.动画文件我们使用atlas形式.在打包发布或者模拟器测试的时候,它会将整个.atlas文件夹下的图片打包成一张 ...

  10. MySQL 常用函数列表

    一.数学函数 select SQRT (2) --取平方根select ABS (-234) --取绝对值select FLOOR (COUNT (*)/5.0) from news --取小于这个小 ...