码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4405 Aeroplane chess(概率DP)

时间:2015-02-04 16:32:06      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:acm   dp   

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <set>
#define LL long long
#define FOR(i,x,y) for(int i=x;i<=y;i++)
using namespace std;
const int maxn = 100000 + 10;
double dp[maxn];
vector<int> vec[maxn];
int N , M;
int main()
{
    while(scanf("%d%d", &N, &M)!=EOF)
    {
        if(N == 0 && M == 0)
            break;
        FOR(i,1,N) vec[i].clear();
        FOR(i,1,M)
        {
            int x, y;
            scanf("%d %d", &x, &y);
            vec[x].push_back(y);
        }
        memset(dp, 0, sizeof(dp));
        dp[N] = 0;
            for(int i=N-1;i>=0;i--)
            {
                if(vec[i].size() > 0)
                {
                    dp[i] = dp[vec[i][0]];
                }
                else
                {
                    for(int x=1;x<=6;x++)
                    {
                        dp[i] += (1.0 / 6.0) * dp[i+x];
                    }
                    dp[i] += 1;
                }
            }
        printf("%.4lf\n", dp[0]);
    }
    return 0;
}

题目大意:

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.

解题思路:

求概率,依旧是用逆推的方式求解。
dp[i] 表示在 点 i 处走到终点所需要掷骰子的期望次数。
很容易写出递推方程。
当点 i 处可以跳跃到点 j 时 dp[i] = dp[j];
当点 i 处不可以跳跃时, dp[i] = sigma(dp[i+x] / 6)


HDU 4405 Aeroplane chess(概率DP)

标签:acm   dp   

原文地址:http://blog.csdn.net/moguxiaozhe/article/details/43487665

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!