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\n", ++cnt,  Dp[1]);
    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/YDDDD/p/11448526.html