zoukankan      html  css  js  c++  java
  • HDU 4405 Aeroplane chess(期望dp)

    题意:沿着x轴从0走到大于等于N的某处,每一步的步数由骰子(1,2,3,4,5,6)决定,若恰好走到x轴上某飞行路线的起点,则不计入扔骰子数。问从0走到大于等于N的某处的期望的扔骰子次数。

    分析:

    1、dp[i]表示从位置i到终点期望的扔骰子次数。

    2、很显然倒着往前推,因为从起点0开始,扔骰子的次数有很多种可能,难以计算,但是dp[N]很显然是0,不需要扔骰子即可到达终点。

    3、假设当前位于位置i,根据骰子数可能到达的位置有i + j(j=1,2,3,4,5,6),到达其中每个位置的概率都是1/6,

    与此同时继承了从所到达的位置到终点的所需扔的骰子数,即dp[i] += dp[i + j] / 6。

    4、而从位置i到位置i+j是需要扔一次骰子的,所以 ++dp[i]。

    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<iostream>
    #include<sstream>
    #include<iterator>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    #include<deque>
    #include<queue>
    #include<list>
    #define lowbit(x) (x & (-x))
    const double eps = 1e-8;
    inline int dcmp(double a, double b){
        if(fabs(a - b) < eps) return 0;
        return a > b ? 1 : -1;
    }
    typedef long long LL;
    typedef unsigned long long ULL;
    const int INT_INF = 0x3f3f3f3f;
    const int INT_M_INF = 0x7f7f7f7f;
    const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
    const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
    const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
    const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
    const int MOD = 1e9 + 7;
    const double pi = acos(-1.0);
    const int MAXN = 100000 + 10;
    const int MAXT = 10000 + 10;
    using namespace std;
    int f[MAXN];
    double dp[MAXN];
    int main(){
        int N, M;
        while(scanf("%d%d", &N, &M) == 2){
            if(!N && !M) return 0;
            memset(f, 0, sizeof f);
            memset(dp, 0, sizeof dp);
            while(M--){
                int x, y;
                scanf("%d%d", &x, &y);
                f[x] = y;
            }
            for(int i = N - 1; i >= 0; --i){
                if(f[i]) dp[i] = dp[f[i]];
                else{
                    for(int j = 1; j <= 6; ++j){
                        dp[i] += dp[i + j] / 6;
                    }
                    ++dp[i];
                }
            }
            printf("%.4f\n", dp[0]);
        }
        return 0;
    }
    

      

  • 相关阅读:
    如何使用ASP.NET2.0的“嵌入的资源”
    多核心计算机运算
    [翻译]注册协议(Register Protocol)
    [翻译]关于“异步可插协议”(About Asynchronous Pluggable Protocols(APPs))
    [ASP.NET]runat="server" causes the problem (< or &lt;)
    [翻译]将应用程序注册为URL协议(Registering an Application to a URL Protocol)
    【C# 转换编码格式】 唉,总是忘记的一个方法。
    在WinForm中借助WebBrowser控件使用 tinymce 总结
    sqlite 资料整理(一)
    sqlite 性能优化
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6657283.html
Copyright © 2011-2022 走看看