LightOJ1030---Discovering Gold(概率dp)

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.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains a blank line and an integer N (1 ≤ N ≤ 100) denoting the dimension of the cave. The next line contains N space separated integers. The ith integer of this line denotes the amount of gold you will get if you come to the ith cell. You may safely assume that all the given integers will be non-negative and no integer will be greater than 1000.

Output

For each case, print the case number and the expected number of gold you will collect. Errors less than 10-6 will be ignored.

Sample Input

Output for Sample Input

3

1

101

2

10 3

3

3 6 9

Case 1: 101.0000000000

Case 2: 13.000

Case 3: 15

Problem Setter: Jane Alam Jan

dp[i]表示在第i个格子时得到的金子的期望,然后记忆化搜索一下就行了

/*************************************************************************
    > File Name: b.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年04月29日 星期三 19时04分23秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

double dp[110];
int arr[110];
int n;

double dfs(int cur) {
    if (dp[cur] != -1.0) {
        return dp[cur];
    }
    double ans = 0;
    int cnt = 0;
    for (int i = 1; i <= 6; ++i) {
        if (cur + i <= n) {
            ++cnt;
        }
    }
    for (int i = 1; i <= 6;  ++i) {
        if (cur + i <= n) {
            ans += (1.0 / cnt) * dfs(cur + i);
        }
    }
    ans += arr[cur];
    dp[cur] = ans;
    return dp[cur];
}

int main() {
    int t;
    scanf("%d", &t);
    int icase = 1;
    while (t--){
        scanf("%d", &n);
        for (int i = 1; i <= n; ++i) {
            scanf("%d", &arr[i]);
            dp[i] = -1.0;
        }
        dp[n] = arr[n];
        printf("Case %d: %.12f\n", icase++, dfs(1));
    }
    return 0;
}
时间: 2024-11-02 23:25:09

LightOJ1030---Discovering Gold(概率dp)的相关文章

LightOJ1030 Discovering Gold 概率DP 水题

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Description 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.

Light OJ 1030 - Discovering Gold(概率dp)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题目大意:有一个很长的洞穴, 可以看做是1-n的格子.你的起始位置在1的地方, 每个格子中都有价值为v[i]的宝藏. 有一个6面的骰子,数字为从1-6, 每次摇一次骰子, 得到的数字x后, 你可以到达距离当前位置大x的位置, 并且得到那个位置的宝藏. 如果要走的位置在n的外面, 那么在此摇骰子, 直到找到一个合适的数字.到达n位置的时候结束. 现在想知道走到n位置的能够

LightOJ 1030 Discovering Gold (概率/期望DP)

题目链接:LightOJ - 1030 Description You are in a cave, a long cave! The cave can be represented by a \(1 \times 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\) s

LightOJ1030 Discovering Gold(概率DP)

题目大概说一个1×n的格子,每个格子都有一定的黄金,起点在1,终点在n,通过投掷6面骰子前进与骰子点数一样的步数,如果会超过n就重新投,每到一个格子就获得其中的黄金.问到达n能得到的黄金数目的期望. 求概率是正推,求期望是逆推..容我慢慢体会.. 期望: 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 double d[111]; 6 int ma

LightOJ1030 Discovering Gold

题目链接:https://vjudge.net/problem/LightOJ-1030 知识点: 概率与期望 解题思路: 设某一个点 \(i\) 能到达的点的个数为 \(x\),其上有金 \(g\),则该点上的期望 \(f(i) = g + \frac{f(i+1) + f(i+2) + ... + f(i+x)}{x}\). AC代码: 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 const int maxn = 100+5;

lightoj 1030 Discovering Gold[ 期望 ]

B - Discovering Gold Description 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 yo

Light oj 1030 概率DP

D - Discovering Gold Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1030 Description You are in a cave, a long cave! The cave can be represented by a 1

1030 - Discovering Gold

   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB 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

LightOJ 1030 Discovering Gold (期望)

https://vjudge.net/problem/LightOJ-1030 题意: 在一个1×N的格子里,每个格子都有相应的金币数,走到相应格子的话,就会得到该格子的金币. 现在从1格子开始,每次摇骰子,他就前进几步,但有一种情况例外,如果当前位置+色子数 > N,那么他就会重新摇色子. 走到N这个位置的话,意味着游戏结束了. 问游戏结束时,这个人得到金币的期望. 思路:这里给出两种做法,一种是正序求解,一种是逆序求解. ①正序求解: 这种做法是从前往后计算每个格子的概率,假设我们现在处于第