题目链接:https://cn.vjudge.net/problem/LightOJ-1030
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
题意:
给出n个格子,编号为1~n,每个格子里有价值Gi的宝藏;
现在,扔一个六面的标准的骰子,按得到的点数走格子,起点为1;
如果在某个格子,扔出骰子之后,得到的点数会让你走到编号大于n的格子,就不算数,重新扔,直到扔到一个你能走点数为止;
如果你走到了编号n的格子,就停止;
求得到宝藏价值的期望值。
题解:
每个格子的价值为Gi,我们只要求出每个格子可能被走到的概率Pi,那么我们求出Σ( Gi * Pi )即为答案;
显然是个概率DP题,动态转移求出走到每个格子的概率即可;
AC代码:
1 #include<cstdio> 2 #include<cstring> 3 #define MAXN 105 4 #define min(a,b) (a<b)?a:b 5 int n,grid[MAXN]; 6 double dp[MAXN]; 7 int main() 8 { 9 int t; 10 scanf("%d",&t); 11 for(int kase=1;kase<=t;kase++) 12 { 13 scanf("%d",&n); 14 for(int i=1;i<=n;i++) scanf("%d",grid+i); 15 16 memset(dp,0,sizeof(dp)); 17 dp[1]=1; 18 for(int i=1;i<=n;i++) 19 { 20 int k=min(6,n-i); 21 for(int j=1;j<=k;j++) dp[i+j]+=dp[i]*(1.0/k); 22 } 23 24 double ans=0; 25 for(int i=1;i<=n;i++) ans+=dp[i]*grid[i]; 26 printf("Case %d: %.7lf ",kase,ans); 27 } 28 }
PS. becky大佬有期望DP的做法:http://blog.csdn.net/becky_w/article/details/78247858