Probability DP: Flying chess hdu4405

Source: Internet
Author: User

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 has six faces with equal probability to face up and the numbers on the faces is 1, 2,3,4,5,6). When Hzz was at grid I and the dice number is x, he would moves to grid i+x. Hzz finishes the game when I+x is E Qual to or greater than N.

There is also M flight lines on the chess map. The i-th flight line can help Hzz fly from a grid Xi to Yi (0

#include <cstdio>
#include <cstring>
using namespace std;
int n,m,t1,t2, fly[100009];
Double dp[100009];
int main ()
{
    while (scanf ("%d%d", &n,&m) && (n| | m))
    {
        memset (fly,-1,sizeof fly);
        memset (dp,0,sizeof dp);
        while (m--)
        {
            int t2,t1;
            scanf ("%d%d", &t1,&t2);
            fly[t1]=t2;
        }
        for (int i=n-1;i>=0;i--)
        {
            if (fly[i]!=-1)
                Dp[i]=dp[fly[i]];
            else
            {for
                (int j=1;j<=6;j++)
                    dp[i]+=dp[i+j];
                    dp[i]=dp[i]/6+1;
            }
        }
        printf ("%.4f\n", Dp[0]);
    }
    return 0;
}

Ideas:
1, if it is not possible to fly directly from a position can start to six different positions, the probability of the sum to the position, due to six possible equalization of opportunities, so the result is divided by 6 and plus one (no matter where you go will be the first dice;
2, if you can fly directly to the probability of flying to the location;

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.