[ACM] hdu 4405 Aeroplane chess (概率DP)
Aeroplane chess
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.
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.
2 0 8 3 2 4 4 5 7 8 0 0
1.1667 2.3441
解题思路:
题意为一条直线上有N+1个格子,编号0-N,也是其位置,有一枚骰子。比方当前位置你在i,骰子执到了 y点(1<=y<=6),那么下一步你就到了i+y位置,格子里有几个特殊的格子。能够从x位置瞬间转移到y位置,不须要步数,假设转移以后的格子还是特殊的格子,那么继续转移,要求的是,最后到达的位置>=n,所须要的投骰子次数的期望。
我们用 dp[i]表示当前位置到达位置>=n所须要投掷骰子的平均次数。
那么非常明显有 dp[n]=0; 而我们要求的则是dp[0] (起点)
注意两点:
①不遇到特殊格子时,投掷一次筛子,由当前位置i到达位置 ,i+1 , i+2 , i+3 , i+4 , i+5 ,i+6 的概率是一样的。都是 1/6,那么 dp[i]=(dp[i+1] +dp[i+2]+....dp[i+6])/6 +1 (须要多投掷一次筛子。所以+1).
②遇到特殊格子时。当前位置的投掷骰子的平均次数等于转移到的位置的平均次数. 比方由x到y。 那么 dp[x]=dp[y]。
依据以上两点。就能够写出代码.
dp[n]是已知的。须要从后往前推。
代码:
- #include <iostream>
- #include <iomanip>
- #include <string.h>
- using namespace std;
- const int maxn=100005;
- double dp[maxn];
- int hash[maxn];
- int n,m;
- int main()
- {
- while(cin>>n>>m&&(n||m))
- {
- int x,y;
- memset(hash,0,sizeof(hash));
- for(int i=1;i<=m;i++)
- {
- cin>>x>>y;
- hash[x]=y;//标记一下,x位置的格子是特殊格子
- }
- dp[n]=0;
- for(int i=n-1;i>=0;i--)
- {
- if(hash[i])//特殊格子
- dp[i]=dp[hash[i]];
- else
- {
- double temp=0;
- for(int j=1;j<=6&&i+j<=n;j++)
- temp+=dp[i+j]/6;
- dp[i]=temp+1;
- }
- }
- cout<<setiosflags(ios::fixed)<<setprecision(4)<<dp[0]<<endl;
- }
- return 0;
- }
[ACM] hdu 4405 Aeroplane chess (概率DP)的更多相关文章
- HDU 4405 Aeroplane chess 概率DP 难度:0
http://acm.hdu.edu.cn/showproblem.php?pid=4405 明显,有飞机的时候不需要考虑骰子,一定是乘飞机更优 设E[i]为分数为i时还需要走的步数期望,j为某个可能 ...
- HDU 4405 Aeroplane chess (概率DP)
题意:你从0开始,要跳到 n 这个位置,如果当前位置是一个飞行点,那么可以跳过去,要不然就只能掷骰子,问你要掷的次数数学期望,到达或者超过n. 析:概率DP,dp[i] 表示从 i 这个位置到达 n ...
- HDU 4405 Aeroplane chess(概率dp,数学期望)
题目 http://kicd.blog.163.com/blog/static/126961911200910168335852/ 根据里面的例子,就可以很简单的写出来了,虽然我现在还是不是很理解为什 ...
- HDU 4405 Aeroplane chess 期望dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Time Limit: 2000/1000 MS (Java/ ...
- hdu 4405 Aeroplane chess (概率DP)
Aeroplane chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 4405 Aeroplane chess(概率+dp)
Problem Description Hzz loves aeroplane chess very much. The chess map contains N+ grids labeled to ...
- hdu 4405 Aeroplane chess(简单概率dp 求期望)
Aeroplane chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU 4405 Aeroplane chess:期望dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4405 题意: 你在下简化版飞行棋... 棋盘为一个线段,长度为n. 上面有m对传送门,可以直接将你从a ...
- HDU4405 Aeroplane chess (概率DP,转移)
http://acm.hdu.edu.cn/showproblem.php?pid=4405 题意:在一个1×n的格子上掷色子,从0点出发,掷了多少前进几步,同时有些格点直接相连,即若a,b相连,当落 ...
随机推荐
- Jquery学习笔记:事件处理基础介绍
一.引子 给html的元素添加一个响应事件,最简单的办法是直接在元素标签内填写事件属性,先看一个最简单的例子 <!DOCTYPE html> <html lang="zh- ...
- UIView详解1
一个UIView的实例就是一个视图,表示的是屏幕上的一块矩形区域,负责这块矩形区域的描绘以及和用户的交互. 第一.UIView的可视化属性 1. backgroundColor 背景属性 2. hi ...
- 基于visual Studio2013解决C语言竞赛题之1005整理队形
题目 解决代码及点评 /************************************************************************/ ...
- tcp接收xml数据解析
避免tcp接收xml数据时加上xml数据长度,根据xml数据特点来解析recv到的xml数据 int nPos1 = 0; int nPos2 = 0; int nTempPos = 0; int n ...
- <转载>网页设计中的F式布局
地址:http://www.uisdc.com/understanding-the-f-layout-in-web-design 网页设计中的F式布局 今天我们来重点介绍网页设计中的F式布局.传统的布 ...
- 开源数据库连接池之Tomcat内置连接池
本篇介绍几种开源数据库连接池,同时重点讲述如何使用Tomcat服务器内置的数据库连接池. 之前的博客已经重点讲述了使用数据库连接池的好处,即是将多次创建连接转变为一次创建而使用长连接模式.这样能减少数 ...
- XFdtd 7.3.2发布增强生物电磁学中的核磁共振功能
XFdtd 日前发布7.3.2版,该版本主要针对生物电磁学中的核磁共振(MR)进行了功能增强,另外,也对软件的用户体验和计算性能进行了部分更新. XFdtd 是基于时域有限差分(FDTD)方法的全波三 ...
- Google App Engine 学习和实践
这个周末玩了玩Google App Engine,随手写点东西,算是学习笔记吧.不当之处,请多多指正. 作者:liigo,2009/04/26夜,大连 原创链接:http://blog.csdn.ne ...
- JavaScript编程:使用DOM操作样式表
6.使用DOM操作样式表: 操纵元素的Style样式属性: background-color:style.backgroundColor color:style.col ...
- gbs remotebuild使用说明
本文件从:https://source.tizen.org/documentation/articles/gbs-remotebuild翻译而来. 1 远程构建 使用remotebuild子指令将本地 ...