Aeroplane chess
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/others) total submission (s): 1581 accepted submission (s): 1082
Problem descriptionhzz loves aeroplane chess very much. the chess map contains N + 1 grids labeled from 0 to n. hzz starts at grid 0. for each step he throws a dice (a dice have six faces with equal probability to face up and the numbers on the faces are 1, 2, 4, 5, 6 ). when hzz is at grid I and the dice number is X, he will moves to grid I + X. hzz finishes the game when I + X is equal to or greater than N.
There are also m flight lines on the chess map. the I-th flight line can help hzz fly from grid XI to Yi (0 <xi <Yi <= N) without throwing the dice. if there is another flight line from Yi, hzz can take the flight line continuously. it is granted that there is no two or more flight lines start from the same grid.
Please help hzz calculate the expected dice throwing times to finish the game. inputthere are multiple test cases. each test case contains several lines. the first line contains two integers n (1 ≤ n ≤ 100000) and M (0 ≤ m ≤ 1000 ). then M lines follow, each line contains two integers Xi, Yi (1 ≤ xi <Yi ≤ n ). the input end with n = 0, m = 0. outputfor each test case in the input, you should output a line indicating Expected dice throwing times. output shoshould be rounded to 4 digits after decimal point. sample input2 08 32 44 57 80 0 sample output1.16672.3441 source 2012 ACM/ICPC Asia Regional Jinhua online has a long board that can jump between 1, 2, 3, 4, 5, and 6 each time, the probability of each hop is equal to 1/6, and there is a flight at a certain point that can help him to jump directly to Y and ask n how many times he needs to jump on average. DP [N] = 0; this is known as where to Skip, therefore, the DP [I] = sum {1 + dp [n + J]} (1 <= j <= 6) code can be regressed:
1 #include<cstdio> 2 #include<cstring> 3 #include<stack> 4 #define maxn 100008 5 using namespace std; 6 double dp[maxn]; 7 stack<int> path[maxn]; 8 int pa[maxn]; 9 int main()10 {11 int n,m,a,b;12 while(scanf("%d%d",&n,&m)!=EOF&&n+m)13 {14 memset(pa,0,sizeof(int)*(n+2));15 memset(dp,0,sizeof(double)*(n+8));16 while(m--)17 {18 scanf("%d%d",&a,&b);19 path[b].push(a);20 pa[a]=b;21 }22 while(--n>=0)23 {24 while(!path[n+1].empty())25 {26 dp[path[n+1].top()]=dp[n+1];27 path[n+1].pop();28 }29 if(pa[n]==0)30 dp[n]=1.0+(dp[n+1]+dp[n+2]+dp[n+3]+dp[n+4]+dp[n+5]+dp[n+6])/6.0;31 }32 printf("%.4lf\n",dp[0]);33 }34 return 0;35 }
View code
HDU 4405 aeroplane chess (probability DP)