Aeroplane chess

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1503    Accepted Submission(s): 1025

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推荐一个链接: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)的更多相关文章

  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)

    Problem Description Hzz loves aeroplane chess very much. The chess map contains N+ grids labeled to ...

  7. hdu 4405 Aeroplane chess(简单概率dp 求期望)

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  8. 【刷题】HDU 4405 Aeroplane chess

    Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled fr ...

  9. HDU 4405 Aeroplane chess (概率DP求期望)

    题意:有一个n个点的飞行棋,问从0点掷骰子(1~6)走到n点须要步数的期望 当中有m个跳跃a,b表示走到a点能够直接跳到b点. dp[ i ]表示从i点走到n点的期望,在正常情况下i点能够到走到i+1 ...

随机推荐

  1. UVAlive 6131 dp+斜率优化

    这道题和06年论文<从一类单调性问题看算法的优化>第一道例题很相似. 题意:给出n个矿的重量和位置,这些矿石只能从上往下运送,现在要在这些地方建造m个heap,要使得,sigma距离*重量 ...

  2. TypedArray和obtainStyledAttributes使用

    在编写Android自定义按钮示例基础上,如果要指定字体大小产生这样的效果: 其实是不需要自定义变量的,可以直接使用TextView的配置属性: <com.easymorse.textbutto ...

  3. 2d-x中Lua类型强转问题

    在Lua中,使用CCDictionary进行保存CCSprite对象,但是,在CCDictionary取出来的时候,此时是一个CCObject对象,无法调用子类精灵的一些方法.那只能进行强转的. 那么 ...

  4. Linux字符设备驱动file_operations

    struct _file_operations struct _file_operations在Fs.h这个文件里面被定义的,如下所示: struct file_operations { struct ...

  5. 服务 通话录音 TelephonyManager

    MainActivity public class MainActivity extends ListActivity {     private BatteryChangedReceiver rec ...

  6. [MVC4-基礎] 從資料庫取值顯示在DropDownList中

    剛開始學MVC4,以下是一些基礎的學習筆記! 完成效果像下面這樣,資料來源是既有的Database. 1.Controller public ActionResult Index() { SqlCon ...

  7. Web ADF 编程步骤.

    从Web Controls 开始(工具来中的 ArcGIS Web Controls). 访问Resource Manager. 找到待访问的 Resource. 决定 Resource支持哪个 Fu ...

  8. winform中获取Properties窗口的值.

    我写这个工具,主要是多次在将自己的代码和别人代码做对比时,不想繁琐地用眼看他设置的和自己设置的哪里不一样. using System; using System.Collections.Generic ...

  9. js大文件分割上传

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  10. C语言---volatile(我的工程笔记本)

    一般说来,volatile用在如下的几个地方: 1.中断服务程序中修改的供其它程序检测的变量需要加volatile: 2.多任务环境下各任务间共享的标志应该加volatile: 3.存储器映射的硬件寄 ...