1030 - Discovering Gold (lightoj 1030 概率DP)

本文探讨了一种在限定条件下的寻宝问题:在一个长度为N的洞穴中,每格含有不同数量的黄金。通过掷骰子来决定前进位置并收集黄金。文章详细解释了如何计算在特定条件下预期可以收集到的黄金总量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1030 - Discovering Gold
Time Limit: 2 second(s)Memory Limit: 32 MB

You are in a cave, a long cave! The cave can be representedby 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 youthrow a perfect 6 sided dice. If you get X in the dice afterthrowing, you add X to your position and collect all the gold from thenew position. If your new position is outside the cave, then you keep throwingagain until you get a suitable result. When you reach the Nthposition you stop your journey. Now you are given the information about thecave, you have to find out the expected number of gold you can collectusing 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 linecontains N space separated integers. The ith integerof this line denotes the amount of gold you will get if you come to the ithcell. You may safely assume that all the given integers will be non-negativeand no integer will be greater than 1000.

Output

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

设在i处的期望为E(i)


E(i) = (E(i+1) + E(i+2) + E(i+3) + E(i+4) + E(i+5) + E(i+6)) / 6 + gold[i];

逆向递推就可以了


/***********************************************
 * Author: fisty
 * Created Time: 2015-08-04 21:09:25
 * File Name   : 1030.cpp
 *********************************************** */
#include <iostream>
#include <cstring>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <algorithm>
using namespace std;
#define Debug(x) cout << #x << " " << x <<endl
#define Memset(x, a) memset(x, a, sizeof(x))
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef pair<int, int> P;
#define FOR(i, a, b) for(int i = a;i < b; i++)
#define lson l, m, k<<1
#define rson m+1, r, k<<1|1
#define MAX_N 110
int t;
int n;
int a[MAX_N];
double dp[MAX_N];
int cnt;
int main() {
    //freopen("in.cpp", "r", stdin);
    //cin.tie(0);
    //ios::sync_with_stdio(false);
    scanf("%d", &t);
    cnt = 1;
    while(t--){
        scanf("%d", &n);
        FOR(i, 1, n+1){
            scanf("%d", &a[i]);
        }
        dp[n] = a[n];
        for(int i = n - 1;i >= 1; i--){
            dp[i] = a[i];
            int d;
            if((n-i) < 6) d = n-i;
            else d = 6;
            for(int j = 1;j <= d; j++){
                dp[i] += dp[i+j] / d;
            }
        }
        printf("Case %d: ", cnt++);
        printf("%.10lf\n", dp[1]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值