HDU 4405 Aeroplane chess (probability DP expectation), hdu4405
Q: I have a Flying chess game with n points. I want to roll the dice from 0 points (1 ~ 6) Expectation of steps required to reach n points
There are m hops a, and B indicates that you can jump directly to B when you go to.
Dp [I] indicates the expectation from I to n. Under normal circumstances, I can go to I + 1, I + 2, I + 3, I + 4, I + 5, I + 6 points, and the probability of each point is 1/6
Therefore, dp [I] = (dp [I + 1] + dp [I + 2] + dp [I + 3] + dp [I + 4] + dp [I + 5] + dp [I + 6]) /6 + 1 (step 1 ).
For vertices with jumps, the value is dp [a] = dp [B].
#include<stdio.h>#include<string.h>#include<string>#include<map>#include<stack>#include<math.h>#include<queue>#include<vector>#include<stdlib.h>#include<iostream>#include<algorithm>using namespace std;#define maxn 100000map<int,int> mp;int main(){int n,m;double dp[maxn];while(scanf("%d%d",&n,&m)){if(n==0&&m==0) break;int a,b;for(int i=0;i<=n;i++)mp[i]=-1;while(m--){scanf("%d%d",&a,&b);mp[a]=b;}memset(dp,0.0,sizeof(dp));for(int i=n-1;i>=0;i--){if(mp[i]!=-1) dp[i]=dp[mp[i]];else{for(int j=1;j<=6;j++)dp[i]+=dp[i+j];dp[i]=dp[i]/6+1;}}printf("%.4lf\n",dp[0]);}return 0;}/*2 08 32 44 57 80 0*/