Mary stands in a strange maze, the maze looks like a triangle(the first layer have one room,the second layer have two rooms,the third layer have three rooms …). Now she stands at the top point(the first layer), and the KEY of this maze is in the lowest layer’s leftmost room. Known that each room can only access to its left room and lower left and lower right rooms .If a room doesn’t have its left room, the probability of going to the lower left room and lower right room are a and b (a + b = 1 ). If a room only has it’s left room, the probability of going to the room is 1. If a room has its lower left, lower right rooms and its left room, the probability of going to each room are 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?
There are no more than 70 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 number 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 case is not to be processed.
Please calculate the expected number of steps required to reach the KEY room, there are 2 digits after the decimal point.
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>
10 #include <queue>
11 #include <deque>
12 #include <stack>
13 #include <set>
14 #include <map>
15 #include <cstdio>
16 #include <cstdlib>
17 #include <cmath>
18 #include <cstring>
19 #include <climits>
20 #include <cctype>
21 using namespace std;
22 #define XINF INT_MAX
23 #define INF 0x3FFFFFFF
24 #define MP(X,Y) make_pair(X,Y)
25 #define PB(X) push_back(X)
26 #define REP(X,N) for(int X=0;X<N;X++)
27 #define REP2(X,L,R) for(int X=L;X<=R;X++)
28 #define DEP(X,R,L) for(int X=R;X>=L;X--)
29 #define CLR(A,X) memset(A,X,sizeof(A))
30 #define IT iterator
31 typedef long long ll;
32 typedef pair<int,int> PII;
33 typedef vector<PII> VII;
34 typedef vector<int> VI;
35 double dp[110][110];
36 int main()
37 {
38 ios::sync_with_stdio(false);
39 int n;
40 while(cin>>n&&n){
41 double a,b,c,d,e;
42 cin>>a>>b>>c>>d>>e;
43 CLR(dp,0);
44 for(int i=n;i;i--){
45 for(int j = n+1-i;j<=n;j++){
46 if(i==n&&j==(n+1-i))continue;
47 else if(i==n)dp[i][j]=dp[i][j-1]+1.0;
48 else if(j==(n+1-i))dp[i][j]=a*dp[i+1][j-1]+b*dp[i+1][j]+1.0;
49 else dp[i][j]=c*dp[i+1][j-1]+d*dp[i+1][j]+e*dp[i][j-1]+1.0;
50 }
51 }
52 cout<<fixed<<setprecision(2)<<dp[1][n]<<endl;
53 }
54 return 0;
55 }