[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相连,当落 ...
随机推荐
- UIView详解1
一个UIView的实例就是一个视图,表示的是屏幕上的一块矩形区域,负责这块矩形区域的描绘以及和用户的交互. 第一.UIView的可视化属性 1. backgroundColor 背景属性 2. hi ...
- mysql导入sql文件过大或连接超时的解决的方法
前段时间出差在现场开发的时候,导入数据库老是出问题.最后发现了一个奇妙sql语句交给实施,仅仅要导入出错就把例如以下语句运行就能够了.至今屡试不爽. set global max_allowed_pa ...
- 搭建python集成开发环境.
需要搭建的内容一共有三项, python ,wxpython 以及spe. 其中spe 是python 的可视化集成开发环境(ide) , 其需要python GUI图形库wxpython的支 ...
- ICMP:Internet控制报文协议
ICMP:Internet控制报文协议. 是IP层的组成部分.传递差错报文或其他信息. ICMP报文被封装在IP数据报内部: 详细格式例如以下所看到的: 个字段含义例如以下: 8位类型. 表示该ICM ...
- fzu 1911 Construct a Matrix(矩阵快速幂+规律)
题目链接:fzu 1911 Construct a Matrix 题目大意:给出n和m,f[i]为斐波那契数列,s[i]为斐波那契数列前i项的和.r = s[n] % m.构造一个r * r的矩阵,只 ...
- Axure自动备份功能!让意外不在可怕!
忘记保存了......... 电脑意外重启了............... 不小心删除了.......................... 每次做axure的时候,多有了太多的意外了! 萧何今天在微 ...
- perl 继承小例子
<pre name="code" class="html"><pre name="code" class="ht ...
- 零基础数据分析与挖掘R语言实战课程(R语言)
随着大数据在各行业的落地生根和蓬勃发展,能从数据中挖金子的数据分析人员越来越宝贝,于是很多的程序员都想转行到数据分析, 挖掘技术哪家强?当然是R语言了,R语言的火热程度,从TIOBE上编程语言排名情况 ...
- django admin manytomany获取所选字段值
和一般views中前端数据的获取一样. def save_model(self, request, obj, form, change): door_id_list= request. ...
- Asp.NET调用百度翻译
Asp.NET调用百度翻译,图示: HTML: <%@ Page Language="C#" AutoEventWireup="true" CodeFil ...