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 ...
随机推荐
- JDK版本错误:Unsupported major.minor version 51.0
错误原因 有时候把项目从本机编译文件部署到服务器,或者发给别人使用时,会报如下异常: java.lang.UnsupportedClassVersionError: test_hello_world ...
- somethings about QSplitter
m_splitter = new QSplitter(Qt::Horizontal); m_splitter->addWidget(this->m_leftWidget); m ...
- Cycling Label
Cycling Label 来源: github/BBCyclingLabel Licence: Apache 2.0 作者: Bruno de Carvalho 分类: 标签(Label) 平台: ...
- Python enumerate函数
enumerate函数接受一个可遍历的对象,如列表.字符串,可同时遍历下标(index)及元素值(value) >>> a = ['aaa','bbb','ccc',1235] &g ...
- HTML5 Canvas显示本地图片实例1、Canvas预览图片实例1
1.前台代码: <input id="fileOne" type="file" /> <canvas id="canvasOne&q ...
- 如何启用第三方Chrome插件
如何安装第三方Chrome插件,先下载扩展名为CRX的文件到本地,提醒一下,不能直接在该网站下打开安装,如果安装失败,可以找到此CRX文件拖入到扩展页安装就可以了! 可是,当我们通过本地安装了第三方C ...
- 使用UISegementControl实现简易汤姆猫程序
// // TomViewController.m #import "TomViewController.h" #import <AVFoundation/AVFoundat ...
- HibernateTemplate常用方法总结
HibernateTemplate常用方法 (本文章内容相当于转载自:http://www.tuicool.com/articles/fU7FV3,只是整理了一下内容结构和修改了部分内容,方便阅读) ...
- git基础使用小记
一.安装步骤省略二.运行“Git Bash“在打开的窗口中输入:ssh-keygen -t rsa -C "my@gmail.com" 会提示SSH Public Keys存放的位 ...
- 几个个实用的PHP代码片段【自己备份】
检查服务器是否是 HTTPS 这个PHP代码片段能够读取关于你服务器 SSL 启用(HTTPS)信息. if ($_SERVER['HTTPS'] != "on") { echo ...