Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4405
There are n + 1 points, 0 ~ N. A person is standing at X now. If Flight lines exists at X, he can fly to the corresponding point without rolling the dice. Otherwise, he will move forward to the number on the dice. Asked him the average number of times he needed to throw a dice from.
For the same question type, DP [N] is known to be 0, and then the inverse is performed based on the probability that the current point can reach the next point. DP [0] is the answer.
#include <stdio.h>#include <iostream>#include <map>#include <set>#include <list>#include <stack>#include <vector>#include <math.h>#include <string.h>#include <queue>#include <string>#include <stdlib.h>#include <algorithm>//#define LL __int64#define LL long long#define eps 1e-9#define PI acos(-1.0)using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 4010;double dp[100010];int hash[100010];int main(){ int n,m,u,v; while(~scanf("%d %d",&n,&m)) { if(n == 0 && m == 0) break; memset(dp,0,sizeof(dp)); memset(hash,-1,sizeof(hash)); while(m--) { scanf("%d %d",&u,&v); hash[u] = v; } for(int i = n; i >= 0; i--) { if(i == n) continue;if(hash[i] == -1) { for(int j = i+1; j <= i+6; j++)dp[i] += dp[j];dp[i] /= 6;dp[i] += 1; }elsedp[i] = dp[hash[i]]; } printf("%.4lf\n",dp[0]); } return 0;}
HDU 4405 aeroplane chess (expected)