Q: I have a Flying chess game with N points. I want to roll the dice from 0 points (1 ~ 6) expectations of the number of steps required to reach the N point
There are m hops a. B indicates that the jump to point a can directly jump to point B.
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*/
HDU 4405 aeroplane chess (expectation for probability DP)