Reprint Please specify source: http://www.cnblogs.com/fraud/--by fraud
The number of Stepstime limit:1 Sec Memory limit:128 M Description
Mary stands in a strange maze, the maze looks like a triangle (the first layer has one Room,the second layer has a S,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 (0<n<45), which means the layer of the maze, then Input five real num ber A, B, C, D, E. (0<=a,b,c,d,e<=1, A+b=1, c+d+e=1). The input is terminated with 0. This test is a not-to-be processed. Output
Please calculate the expected number of steps required to reach the KEY, there is 2 digits after the decimal point.
Sample Input
Sample Output
3.41
Probability DP flood problem ...
1#include <iostream>2#include <sstream>3#include <ios>4#include <iomanip>5#include <functional>6#include <algorithm>7#include <vector>8#include <string>9#include <list>Ten#include <queue> One#include <deque> A#include <stack> -#include <Set> -#include <map> the#include <cstdio> -#include <cstdlib> -#include <cmath> -#include <cstring> +#include <climits> -#include <cctype> + using namespacestd; A #defineXinf Int_max at #defineINF 0X3FFFFFFF - #defineMP (x, y) make_pair (x, y) - #definePB (x) push_back (x) - #defineREP (x,n) for (int x=0; x<n; X + +) - #defineREP2 (X,L,R) for (int x=l; x<=r; X + +) - #defineDEP (x,r,l) for (int x=r; x>=l; x--) in #defineCLR (a,x) memset (a,x,sizeof (A)) - #defineIT iterator totypedefLong Longll; +typedef pair<int,int>PII; -typedef vector<pii>VII; thetypedef vector<int>VI; * Doubledp[ the][ the]; $ intMain ()Panax Notoginseng { -Ios::sync_with_stdio (false); the intN; + while(cin>>n&&N) { A Doublea,b,c,d,e; theCin>>a>>b>>c>>d>>e; +CLR (DP,0); - for(inti=n;i;i--){ $ for(intj = n+1-i;j<=n;j++){ $ if(i==n&&j== (n+1-I.))Continue; - Else if(i==n) dp[i][j]=dp[i][j-1]+1.0; - Else if(j== (n+1-i)) dp[i][j]=a*dp[i+1][j-1]+b*dp[i+1][j]+1.0; the Elsedp[i][j]=c*dp[i+1][j-1]+d*dp[i+1][j]+e*dp[i][j-1]+1.0; - }Wuyi } thecout<<fixed<<setprecision (2) <<dp[1][n]<<Endl; - } Wu return 0; -}
13 Shandong Province The number of steps (probability DP water problem)