zoukankan      html  css  js  c++  java
  • LightOJ-1030-Discovering Gold

    链接:

    https://vjudge.net/problem/LightOJ-1030

    题意:

    You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell of the cave can contain any amount of gold.

    Initially you are in position 1. Now each turn you throw a perfect 6 sided dice. If you get X in the dice after throwing, you add X to your position and collect all the gold from the new position. If your new position is outside the cave, then you keep throwing again until you get a suitable result. When you reach the Nth position you stop your journey. Now you are given the information about the cave, you have to find out the expected number of gold you can collect using the given procedure.

    思路:

    DP[i] 表示从i点往n点走的期望值,考虑从某点开始时可以直接拿走对应的黄金。
    同时i点往前延伸min(6, n-i)个点都是有概率走到的,直接计算期望。

    代码:

    #include <iostream>
    #include <memory.h>
    #include <string>
    #include <istream>
    #include <sstream>
    #include <vector>
    #include <stack>
    #include <algorithm>
    #include <map>
    #include <queue>
    #include <math.h>
    #include <cstdio>
    #include <set>
    #include <iterator>
    #include <cstring>
    #include <assert.h>
    using namespace std;
    
    typedef long long LL;
    
    double Dp[110];
    int a[110];
    int n;
    
    int main()
    {
        int t, cnt = 0;
        scanf("%d",&t);
        while (t--)
        {
            memset(Dp, 0, sizeof(Dp));
            scanf("%d", &n);
            for (int i = 1;i <= n;i++)
                scanf("%d", &a[i]);
            Dp[n] = a[n];
            for (int i = n-1;i >= 1;i--)
            {
                Dp[i] += a[i];
                int step = min(6, n-i);
                for (int j = 1;j <= step;j++)
                    Dp[i] += (1.0/step)*Dp[i+j];
            }
            printf("Case %d: %.7lf
    ", ++cnt,  Dp[1]);
        }
    
        return 0;
    }
    
  • 相关阅读:
    给图片加自定义字体水印
    [ZT]互联网网站应该如何存储密码?
    【Best Practice】在JAVA里面反序列化.NET的对象。
    关于权限验证的两篇好文
    U盘安装Ubuntu遇到的奇怪问题
    const 用法
    程序员进级攻略
    搞混的指针数组与数组指针
    C/C++内存分配
    多个常见代码设计缺陷(转载)
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11448526.html
Copyright © 2011-2022 走看看