题干:
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
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这个位置,手里有一颗色子,色子摇到几,他就前进几步,但有一种情况例外,如果当前位置+色子数 > N,那么他就会重新摇色子。
走到N位置,意味着游戏结束了。
问游戏结束时,这个人得到金币的期望。
解题报告:
首先根据期望的线性性转化题意:考虑每个点被选中的概率,走到每个点的概率乘以该点的权值 的和。
dp[i]表示走到i这个点的概率是多少。“我为人人”法转移。(最后的dp[n]肯定==1)
最后乘以权值就是答案。
AC代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int n,a[MAX];
double dp[MAX];
int main()
{int t,iCase=0;cin>>t;while(t--) {scanf("%d",&n);for(int i = 1; i<=n; i++) scanf("%d",a+i);printf("Case %d: ",++iCase);for(int i = 1; i<=n+6; i++) dp[i]=0;dp[1]=1;for(int i = 1; i<=n; i++) {
// for(int j = 1; j<=6; j++) {
// if(i-j >= 1) dp[i] += dp[i-j];
// }int up = min(n-i,6); for(int j = 1; j<=up; j++) dp[i+j] += dp[i]/up;}double ans = 0;for(int i = 1; i<=n; i++) ans += dp[i] * a[i];printf("%.8f\n",ans);}return 0 ;
}
思路2:(链接)
这题要倒着推,由N推向1
设dp[k]为到达k这个位置时得到金币的期望,m为该点和N这个位置的距离,gold[k]为k这个位置的金币数,因为走的位置不能超过N,所以要取min(m,6)
那么dp[k] = 1 / min(m,6) * (dp[k + 1] + dp[k+2] + … + dp[min(m,6)]) + gold[k]。
(其实是因为这题巧了,所以dp[k]可以等概率的由后面的点转移而来,但是第一种方法更具有普遍性)
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
using namespace std;
const int N = 110;
int gold[N];
double dp[N];
int n;double solve() {for(int i = 0; i < n - 1; i++) dp[i] = 0;int step;dp[n-1] = gold[n-1];for(int i = n - 2; i >= 0; i--) {dp[i] = gold[i];step = min(6, n-1-i);for(int j = 1; j <= step; j++)dp[i] += 1.0 / step * dp[i+j];}return dp[0];
}int main() {int test, cas = 1;scanf("%d", &test);while(test--) {scanf("%d", &n);for(int i = 0; i < n; i++)scanf("%d", &gold[i]);printf("Case %d: %.7lf\n", cas++, solve());}return 0;
}