HDU-4405 aeroplane chess expectation DP

Source: Internet
Author: User

DP [I] indicates the expected number of days for the I position to jump out. The positions n + 1 to n + 5 are constructed first, then assign all the six locations DP [N-N + 5] to 0, because these locations have gone out.

Then there is recurrence.

If there is no flight at this point:
DP [x] = (1/6) * (DP [x + 1] + dp [x + 2] + dp [x + 3] + dp [x + 4] + dp [x + 5] + dp [x + 6]) + 1;

Otherwise:
DP [x] = DP [LINK [X]; Link [x] indicates the point to which X is connected.

The Code is as follows:

#include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>#include<cmath>#include<queue>#include<set>#include<map>#include<cstring>#include<vector>#include<string>#define MAXN 100020#define LL long longusing namespace std;int N, M;const double EE = 1.0/6;double dp[MAXN];int link[MAXN];int main(  ){    int len, a, b;    while (scanf("%d %d", &N, &M) == 2) {        len = N+5;        memset(link, 0xff, sizeof (link));        for (int i = N; i <= len; ++i) dp[i] = 0;        for (int i = 1; i <= M; ++i) {            scanf("%d %d", &a, &b);            link[a] = b;        }        for (int i = N-1; i >= 0; --i) {            double temp = 0.0;            if (link[i] != -1) {                temp = dp[link[i]];            } else {                for (int j = i + 1; j <= i + 6; ++j) {                    temp += EE * dp[j];                }                temp += 1;            }            dp[i] = temp;        }        printf("%.4lf\n", dp[0]);    }    return 0;}

 

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.