Backward probability dp expectations

Subject to the effect:

0:00 started from the start point reaches the point n, proceeds through each dice can throw six 1,2,3,4,5,6 cases, several thrown forward few, of course, direct access to the corresponding channel by x-flight point y, x <y, calculate an arrival point, or more than n n

Point mathematical expectation of the number of times you want to throw the dice

After throwing dice from a certain point to reach the i i + 1, i + 2, i + 3, i + 4, i + 5, i + 6 six points, so DP [i] is the mathematical expectation reached the end of the

Then the mathematical expectation of 6 points after reaching the same, then dp [i] = dp [i + 1] * 1 / 6.0 + dp [i + 2] * 1 / 6.0 + dp [i + 3] * 1 / 6.0 + dp [i + 4] * 1 / 6.0 + dp [i + 5] * 1 / 6.0 + dp [i + 6] * 1 / 6.0 + 1

May fly directly hit if a judgment can be given directly to the value dp, dp does not perform the process operation described above

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<math.h>
 4 #include<string.h>
 5 using namespace std;
 6 const int maxn=1e5+10;
 7 int match[maxn];
 8 double dp[maxn];
 9 void init()
10 {
11     memset(dp,0,sizeof(dp));
12     memset(match,0,sizeof(match));
13 }
14 int main()
15 {
16     int n,m;
17     while(scanf("%d%d",&n,&m)!=EOF){
18         if(n==0&&m==0) break;
19         init();
20         for(int i=1;i<=m;i++){
21             int a,b;
22             scanf("%d%d",&a,&b);
23             match[a]=b;
24         }
25         for(int i=n-1;i>=0;i--){
26             if(match[i]) dp[i]=dp[match[i]];
27             else{
28                 for(int j=1;j<=6;j++){
29                     dp[i]+=dp[j+i]/6.0;
30                 }
31                 dp[i]+=1;
32             }
33         }
34         printf("%.4f\n",dp[0]);
35     }
36     return 0;
37 }

 

Guess you like

Origin www.cnblogs.com/pangbi/p/11565163.html