Hdu4405 -- aeroplane chess)

Source: Internet
Author: User

Aeroplane chess Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1628 accepted submission (s): 1103


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


Go from to, get X for each sieve throw, move forward X steps, there are m flight channels, you can not go forward, fly directly, no two flight routes depart from the same point and ask for expectations.

DP [I] = Σ (1/6 * DP [I + J]) + 1;

For a flight route from I to J, it means that when it reaches the position I, it goes to the position J, DP [I] = DP [J]

From the back to the front DP, for each calculated DP, there is no flight route to reach this point. If so, the DP of that point is calculated.

 

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct node{    int u , v ;    int next ;}p[2000];int head[100000] , cnt ;double dp[110000] , k[7];void add(int u,int v){    p[cnt].u = u ;    p[cnt].v = v ;    p[cnt].next = head[v] ;    head[v] = cnt++ ;}int main(){    int n , m , i , j , l , u , v ;    for(i = 1 ; i <= 6 ; i++)        k[i] = 1.0/6 ;    while(scanf("%d %d", &n, &m) && n+m != 0)    {        memset(head,-1,sizeof(head));        for(i = 0 ; i < n ; i++)            dp[i] = -1;        cnt = 0 ;        while(m--)        {            scanf("%d %d", &u, &v);            add(u,v);        }        for(i = n ; i <= n+6 ; i++)            dp[i] = 0 ;        for(l = head[n] ; l != -1 ; l = p[l].next)            dp[ p[l].u ] = dp[n] ;        for(i = n-1 ; i >= 0 ; i--)        {            if( dp[i] != -1 )            {                for(l = head[i] ; l != -1 ; l = p[l].next)                    dp[ p[l].u ] = dp[i] ;                continue ;            }            dp[i] = 1 ;            for(j = 1 ; j <= 6 ; j++)                dp[i] += k[j]*dp[i+j] ;            for(l = head[i] ; l != -1 ; l = p[l].next)                dp[ p[l].u ] = dp[i] ;        }        printf("%.4lf\n", dp[0]);    }    return 0;}


 

Hdu4405 -- aeroplane chess)

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.