LightOJ-1030-Discovering Gold

link:

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

Meaning of the questions:

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.

Ideas:

The DP [i] n represents the point to i point away from a desired value, from the viewpoint of a start point corresponding to directly take gold.
I point forward while extending min (6, ni) is the probability points come directly calculate the desired.

Code:

#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;
}

Guess you like

Origin www.cnblogs.com/YDDDD/p/11448526.html