HDU 4405 Aeroplane chess probability dp
Question: 0 ~ N is n + 1 in total, and each time you throw a six-sided dice, if you get x (1 <= x <= 6) points, then I goes to the I + x lattice. Some grids are airports. As long as they stand on the grid A, they will fly directly from the grid A to the grid B. The game ends when you stand in the k cell and k> = n. Expected number of times of throwing a dice.
Explain the limit imposed by probability dp, and explain why when there is an airplane, the front is equal to the back, because dp represents the expectation from the current position to n, so the probability above is equal to that of the plane
Reprinted:
Another expectation is DP. In fact, this question is similar to the probability DP in hdu4576 (I also wrote a question ). The two channels calculate the probability, and the expectation can be put together for comparison. The expectation and the probability are different.
To sum up one sentence: Generally, the problem of probability DP or recurrence is being pushed, from the initial state to the end state (the end state is generally the State required for such questions, therefore, the first (or first known) state is near the initial state, so the current state to be requested is pushed from the first N possible states that can be transferred to this state; in general, the expectation DP is always pushed back, from the end state to the initial state (the initial state is often the status required for such questions), so first get (or first known) the status is close to the end state, so the current state to be requested is pushed from this state to the next N possible states.
N + 1 points on the number axis (0 ~ N) a single player starts from 0 and ends when it reaches N or greater than N. Each time you roll the dice, the dice number is 1-6, and the number of throws is a few steps forward. There are some special points on this number axis. These points are similar to the flying points in the Flying chess, as long as you reach these points, you can fly directly to the given point. Expected Total number of times of throwing dice.
For example, this question is analyzed in reverse. Dp [I] indicates the expected number of throws from the end of the game at position I. Obviously, dp [n] is 0, and dp [0] is required. For vertices that fly directly. For example, the array vis [] is used to represent vis [a] = B, indicating that vis [a] can fly directly to point B when it reaches Point, obviously dp [vis [a] = dp [a]. In reverse push, dp [I] (assuming that the point is not a Flying Point) has six possible states (corresponding to six possible number of dice ), each type has a 1/6 probability. Therefore, for (int x = 1; x <= 6; x ++) dp [I] + = dp [I + x]/6.0; dp [I] + = 1; note that after adding the expected values for each possibility, you need to add 1, because these six possibilities add up to the expectation of the next state, and the current State is their previous state, therefore, the expected number of times (directly understood as the number of times the dice are throttled) must be + 1.
Description
Hzz 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
Please help Hzz calculate the expected dice throwing times to finish the game.
Input
There 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 The input end with N = 0, M = 0.
Output
For each test case in the input, you should output a line indicating the expected dice throwing times. Output shocould be rounded to 4 digits after decimal point.
Sample Input
2 08 32 44 57 80 0
Sample Output
1.16672.3441
#include
#include
#include#include
#include
using namespace std;int f[100005];double dp[100005];int main(){ int n,m; while(~scanf("%d %d",&n,&m)) { if(n==0&&m==0) break; memset(f,0,sizeof(f)); for(int i=0; i
=0; i--) { if(f[i]) { dp[i]=dp[f[i]]; } else { for(int j=i+1; j<=i+6; j++) dp[i]+=dp[j]; dp[i]=dp[i]/6+1; } } printf("%.4f\n",dp[0]); } return 0;}