Discovering Gold(概率DP)

You are in a cave, a deep cave! The cave can be represented by an 1 x N grid. Some cells in the cave might contain gold!

Initially, you are in position 1. In each move, 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 of the cave, you keep throwing the dice again until you get a suitable result. When you reach the Nth position you stop your journey.

Given the information about the cave, you have to find the expected amount of gold you can collect using the procedure described above.

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 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 not 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 beignored.

Sample Input

3
1
101
2
10 3
3
3 6 9

Sample Output

Case 1: 101.0000000000
Case 2: 13.000
Case 3: 15

 题目大意

有一个1×N的山洞,每个格子有一定的金子。你刚开始位于节点1,并有一个6面的均匀筛子,当掷色子到数字x就能往前走x步,若筛子显示的位置在山顶外则重新掷以使自己在合理范围内移动。

概率DP

f[i]表示到达i\点的概率,递推求解,若此时到终点距离小于6则需要特判

最后结果即a[i[*f[i]累加得期望(细节见代码)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>

using namespace std;

typedef long long ll;
const int N = 1e2 + 5;
double a[N], f[N];//此题不能再加一维表示操作次数,因为最后一步不知道可能会操作多少次 
//f[i]表示到达某点的概率,正好能乘以a[i]权值求和算期望 
int main()
{
	int T, tot;
	scanf("%d", &T);
	tot = T;
	while(T--)
	{
		printf("\n");
		int n;
		scanf("%d", &n);
		for(int i = 1; i <= n; i++)
			scanf("%lf", &a[i]);
		memset(f, 0, sizeof(f));
		f[1] = 1;//边界 
		for(int i = 1; i < n; i++)
			for(int j = i + 1; j <= i + 6 && j <= n; j++)
			{
				if(i + 6 <= n)
					f[j] += f[i] / 6;//递推求概率 
				else
				{
					int t = n - i;//此时只有n-i种情况 
					f[j] += f[i] / t;
				}
			}
		double ans = 0;
		for(int i = 1; i <= n; i++)
			ans += f[i] * a[i];//算期望 
		printf("Case %d: %.10lf\n", tot - T, ans);
	}
return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值