Sdut2623--the Number of steps

Source: Internet
Author: User

The number of steps Time limit:1000ms Memory limit:65536k have questions? Dot here ^_^ Title Description

    Mary stands in a Strange Maze, The maze looks like a triangle (the first layer has one room,the second layer has both rooms,the third layer has three rooms ... ). Now she stands on the top point (the first layer), and the KEY of this maze are in the lowest layer ' s leftmost. Known that each of the lower can only have access to their left and the left and the lower right rooms. If a doesn ' t has its left-of-the-class, the probability of going to the lower left and lower right-class is a and B (a + b = 1). If A, the probability of going to the class is 1. If A is a-lower left, lower-rooms and its-left, the probability of going is C, D, E (c + D + e = 1). Now, Mary wants to know how many steps she needs to reach the KEY. Dear friend, can you tell Mary the expected number of steps required to reach the KEY?


Inputthere is no more than test cases.in each case, first Input a positive integer n (0The input is terminated with 0. This test is a not-to-be processed.Outputcalculate the expected number of steps required to reach the KEY, there is 2 digits after the decimal PO Int.Sample input
Sample output
3.41
Hint Source 2013 The first ACM University student Program design contest in Shandong Province sample program
    • Submit
    • State
    • Discuss

Water problem probability DP


/************************************************************************* > File Name:sdut2623.cpp > Author: ALex > Mail: [email protected] > Created time:2014 December 28 Sunday 15:45 25 seconds ****************************** /#include <map> #include <set> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm>using namespace std;double A, B, C, D, e;double dp[ 1000][1000];int n;double dfs (int i, int j) {if (i > N | | i < 1 | | J < 1 | | J > i) {return 0;} if (dp[i][j]! =-1) {return dp[i][j];} if (J <= 1)//No Left room {Dp[i][j] = a * DFS (i + 1, j) + b * DFS (i + 1, j + 1) + 1;} Else{if (i = = N) {Dp[i][j] = DFS (i, j-1) + 1;} ELSE{DP[I][J] = e * DFS (i, j-1) + c * DFS (i + 1, j) + D * DFS (i + 1, j + 1) + 1;}} printf ("dp[%d][%d] =%.2f\n", I, J, Dp[i][j]); return DPI [j];} int main () {while (~SCANF ("%d", &n), N) {for (int i = 0; I <= N; ++i) {for (int j = 0; J <= N; ++j) {Dp[i][j] =-1;} }DP[N][1] = 0;scanf ("%lf%lf%lf%lf%lf", &a, &b, &c, &d, &e);p rintf ("%.2f\n", DFS (1, 1));} return 0;}


Sdut2623--the Number of steps

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.