首页 > ACM题库 > HDU-杭电 > HDU 4405-Aeroplane chess-动态规划-[解题报告]HOJ
2015
05-24

HDU 4405-Aeroplane chess-动态规划-[解题报告]HOJ

Aeroplane chess

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

2 0
8 3
2 4
4 5
7 8
0 0

样例输出:

1.1667
2.3441

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4405

 

题意:飞行棋,从0到n,置骰子,置到几就往前走几步,前进中会有捷径,比如2和5连到一起了,那你走到2时可以直接跳到

5,如果5和8连到一起了,那你还可以继续跳到8,最后问跳到n时平均置几次骰子。也就是求期望。

 

全期望公式:http://zh.wikipedia.org/wiki/%E5%85%A8%E6%9C%9F%E6%9C%9B%E5%85%AC%E5%BC%8F

全概率公式:http://zh.wikipedia.org/wiki/%E5%85%A8%E6%A6%82%E7%8E%87%E5%85%AC%E5%BC%8F

概率期望学习:http://kicd.blog.163.com/blog/static/126961911200910168335852/

 

#include <iostream>
#include <string.h>
#include <stdio.h>

using namespace std;
const int N=100005;

struct node
{
    int y,next;
};

bool vis[N];
node path[N];
int first[N],t;
double dp[N];

void add(int x,int y)
{
    path[t].y=y;
    path[t].next=first[x];
    first[x]=t++;
}

int main()
{
    double s;
    int n,m,v;
    while(cin>>n>>m)
    {
        if(m==0&&n==0) break;
        memset(dp,0,sizeof(dp));
        memset(vis,0,sizeof(vis));
        memset(first,0,sizeof(first));
        int x,y;
        t=1;
        while(m--)
        {
            cin>>x>>y;
            add(y,x);
        }
        dp[n]=-1;
        for(int i=n; i>=0; i--)
        {
            if(!vis[i])
            {
                vis[i]=true;
                s=0;
                for(int k=1; k<=6; k++)
                    s+=dp[i+k];
                s/=6;
                dp[i]+=(s+1);
            }
            for(int k=first[i]; k; k=path[k].next)
            {
                v=path[k].y;
                dp[v]=dp[i];
                vis[v]=true;
            }
        }
        printf("%.4lf\n",dp[0]);
    }
    return 0;
}

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/acdreamers/article/details/11058581