hdu 4405 Aeroplane chess (概率DP)
Aeroplane chess
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1503 Accepted Submission(s): 1025
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
学习概率DP推荐一个链接:http://kicd.blog.163.com/blog/static/126961911200910168335852/
思路:由当前点能够走向以下6个相邻位置,走到这几个点的概率均相等。用dp[i]表示该点走到目标的期望步数,则该点的期望能够由它能够到达的6个点相加得到,由于它走到下一个位置花费时间1,故要加一。见式子:
dp[0]=dp[1]*1/6+dp[2]*1/6+dp[3]*1/6+dp[4]*1/6+dp[5]*1/6+dp[6]*1/6+1;
dp[n]=0(自身到自身期望为0)
那么,我们倒着推过来就能得到答案为dp[0]。
#include"stdio.h"
#include"string.h"
#include"iostream"
#include"algorithm"
#include"math.h"
#include"vector"
using namespace std;
#define LL __int64
#define N 100005
#define max(a,b) (a>b? a:b)
vector<int>g[N];
int vis[N];
double dp[N];
int main()
{
int n,m,i,j,v,a,b;
while(scanf("%d%d",&n,&m),n||m)
{
for(i=0;i<=n;i++)
g[i].clear();
for(i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
g[b].push_back(a);
}
memset(dp,0,sizeof(dp)); //易知dp[n]=0
memset(vis,0,sizeof(vis));
for(i=0;i<g[n].size();i++)
{
v=g[n][i];
dp[v]=dp[n];
vis[v]=1;
}
for(i=n-1;i>=0;i--)
{
if(!vis[i])
{
for(j=i+1;j<=i+6;j++)
{
dp[i]+=dp[j]/6;
}
dp[i]+=1;
}
for(j=0;j<g[i].size();j++)
{
v=g[i][j];
dp[v]=dp[i];
vis[v]=1;
}
}
printf("%.4f\n",dp[0]);
}
return 0;
}
hdu 4405 Aeroplane chess (概率DP)的更多相关文章
- [ACM] hdu 4405 Aeroplane chess (概率DP)
Aeroplane chess Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 ...
- HDU 4405 Aeroplane chess (概率DP)
题意:你从0开始,要跳到 n 这个位置,如果当前位置是一个飞行点,那么可以跳过去,要不然就只能掷骰子,问你要掷的次数数学期望,到达或者超过n. 析:概率DP,dp[i] 表示从 i 这个位置到达 n ...
- HDU 4405 Aeroplane chess 概率DP 难度:0
http://acm.hdu.edu.cn/showproblem.php?pid=4405 明显,有飞机的时候不需要考虑骰子,一定是乘飞机更优 设E[i]为分数为i时还需要走的步数期望,j为某个可能 ...
- 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)
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
Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled fr ...
- HDU 4405 Aeroplane chess (概率DP求期望)
题意:有一个n个点的飞行棋,问从0点掷骰子(1~6)走到n点须要步数的期望 当中有m个跳跃a,b表示走到a点能够直接跳到b点. dp[ i ]表示从i点走到n点的期望,在正常情况下i点能够到走到i+1 ...
随机推荐
- 部署hibernate框架项目时出现问题:The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files.
基本情况: (这些其实关系不大)我是直接impor导入HibernateDemo项目到eclipse中的,该项目的hibernate版本是3.6.7.Final版,使用了Hibernate Tools ...
- Mac下如何不借助第三方工具实现NTFS分区的可写挂载
问题背景 我想很多使用Mac的同学都会遇到读写NTFS磁盘的问题,因为默认情况下Mac OSX对NTFS磁盘的挂载方式是只读(read-only)的,因此把一个NTFS格式的磁盘插入到Mac上,是只能 ...
- (转)SQL Server 2008怎样编辑200行以上的数据
刚换SQL Server2008 不久,感觉运行速度.编辑提示都比05版的提升不少,但是在维护考试系统中遇到一个05中没有的问题:05中有“打开表”可以编辑所有数据行,到了08变成了“打开前1000行 ...
- ReportViewer导出功能筛选
ReportViewer只能导出Excel,把导出Word和PDF功能去掉 <rsweb:ReportViewer ID="ReportViewer1" runat=&quo ...
- JS 函数参数
1.简单的无参函数调用 function Test1(Func) { Func(); } function Test2() { alert("我要被作为函数参数啦!"); } // ...
- Action<T> 和 Func<T> 委托
概述: 除了为每个参数和返回类型定义一个新委托类型之外,可以使用Action<T> 和 Func<T> 委托. Action<T> Action<T>委 ...
- ORA-01172 ORA-01151
今天发现服务器的9号硬盘坏了,因做了RAID5,报障后也没理会,过了一会儿,有人反应说报表运行不出来,发现服务器所有的硬盘都不工作了,界面也没有反应,就强行关机,再开机可以进入系统,等维修人员来换了新 ...
- shell中对于命令的搜寻顺序
当你在shell命令行输入一条命令时,shell的搜寻顺序是如何的呢?当你的脚本名字和shell中的函数名字重名,shell是如何决定运行哪一个的? 在shell中,shell对于命令的搜寻优先级为: ...
- Java中int和String互相转换的多种方法
1 如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([ ...
- DEDE首页调用{dede:field.content/}
过滤DEDE后台自己添加的Class文件和HTML代码: {dede:sql sql='Select content from #@__arctype where id=1'} [field:cont ...