【刷题】HDU 4405 Aeroplane chess

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

Description(CHN)

在一个 \(1*n\) 的格子上掷色子,从 \(0\) 点出发,掷了多少前进几步,同时有些格点直接相连,即若 \(a\)\(b\) 相连,当落到 \(a\) 点时直接飞向 \(b\) 点。求走到 \(n\) 或超出 \(n\) 期望掷色子次数
\(n≤100000\)

Solution

期望倒推
\(f[i]\) 表示当前到达第 \(i\) 号点,距离游戏结束的期望是多少
显然,\(f[n]=f[n+1]=...=f[n+5]=0\)
然后反着枚举 \(i\) ,如果当前点有直通机,就直接等于直通的那个点的期望
否则,枚举这一次骰子的点数,\(f[i]=\sum_{x=1}^6\frac{f[i+x]+1}{6}\)

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=100000+10;
int n,m,fly[MAXN];
db f[MAXN];
template<typename T> inline void read(T &x)
{
    T data=0,w=1;
    char ch=0;
    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-')w=-1,ch=getchar();
    while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
    x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)write(x/10);
    putchar(x%10+'0');
    if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(!n&&!m)break;
        memset(fly,-1,sizeof(fly));
        for(register int i=1;i<=m;++i)
        {
            int u,v;read(u);read(v);
            fly[u]=v;
        }
        for(register int i=0;i<=n+5;++i)f[i]=0.0;
        for(register int i=n-1;i>=0;--i)
            if(!(~fly[i]))
                for(register int x=1;x<=6;++x)f[i]+=f[i+x]/6.0+1/6.0;
            else f[i]=f[fly[i]];
        printf("%.4f\n",f[0]);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/hongyj/p/9163500.html