HDU 4403 (Aeroplane chess, expectation, probability DP)

Source: Internet
Author: User


Aeroplane chess Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)


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 shocould 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
 
Source2012 ACM/ICPC Asia Regional Jinhua online
Recommendzhoujiaqi2010

Question:

Feiqi. To give a group of data n, m, n represents n + 1 (one-dimensional, 0-> N) grids. Your starting point is at location 0, M indicates that you have m flights. Next there will be m flights. Each line has two integers x and y, indicating that there is a flight at location X and location y, and you can fly directly from X to Y, when you throw a dice, You can take as many steps as you throw it. If you encounter a flight, you can follow the flight and the flight can be continuous. The starting point of each flight is different. Output the expected number of throws.

Solution:

DP [N] = 0, DP [I] = sum (DP [I + J]) + 1, J is accumulated from 1 to 6, because it is expected to represent the number of steps, therefore, when one step is added, when a flight (X, Y) is encountered, DP [x] = DP [y] is recorded, and the result is equal to DP [0].

Code:

#include<iostream>#include<cstdio>using namespace std;const int maxN=100010;int main(){    int a,b,n,m,visited[maxN];    double dp[maxN];    while(~scanf("%d%d",&n,&m)&&(n||m)){        for(int i=0;i<n;i++){            visited[i]=-1;        }         for(int i=0;i<=n+5;i++){            dp[i]=0;        }        for(int i=0;i<m;i++){            scanf("%d%d",&a,&b);            visited[a]=b;        }        for(int i=n-1;i>=0;i--){            if(visited[i]==-1){                for(int j=1;j<=6;j++){                    dp[i]=dp[i+j]/6.0+dp[i];                }                dp[i]+=1;            }            else dp[i]=dp[visited[i]];        }        printf("%.4lf\n",dp[0]);    }    return 0;}

 



HDU 4403 (Aeroplane chess, expectation, probability DP)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.