Description
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?
Input
Output
Please calculate the expected number of steps required to reach the KEY room, there are 2 digits after the decimal point.
Sample Input
30.3 0.70.1 0.3 0.60
Sample Output
3.41
本题的题意十分明显,状态转移也已经明确给出,作为初识概率dp把。有一点要注意概率转移步长加1
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define INF (1<<30)
#define prln(x) printf("%lld...\n",x)
#define pr(x) printf("%lld...",x)
#define EPS 1e-10
#define rep(i,n) for(int (i)=(0);(i)<(n);(i)++)
#define Rep(i,n) for(int (i)=(1);(i)<=(n);(i)++)
const int maxn = 50;
double dp[maxn][maxn];
double a,b,c,d,e;
int n;
int main()
{
while(scanf("%d",&n)==1 && n){
scanf("%lf %lf %lf %lf %lf",&a,&b,&c,&d,&e);
for(int i=n;i>=1;i--)
for(int j=1;j<=i;j++){
if(i == n) dp[i][j] = (j-1);
else {
if(j == 1) dp[i][j] = a*(dp[i+1][j]+1)+b*(dp[i+1][j+1]+1);
else dp[i][j] = c*(dp[i+1][j]+1)+d*(dp[i+1][j+1]+1)+e*(dp[i][j-1]+1);
}
}
printf("%.2lf\n",dp[1][1]);
}
return 0;
}