2225: The number of steps
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 25 Solved: 20
[ Submit][ Status][ Web Board]
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
HINT
Source
题意:在一个三角形内,第n行有n个房间,一个人在最上方开始,每次可能往左走(如果可以),往左下走,往右下走,问到最左下角的期望是多少。
思路:逆向递推。
AC代码如下:
#include<cstdio>
#include<cstring>
using namespace std;
double num[50][50];
int main()
{ int n,i,j,k;
double a,b,c,d,e;
while(~scanf("%d",&n) && n>0)
{ scanf("%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e);
for(i=1;i<=n;i++)
num[n][i]=i-1;
for(i=n-1;i>=1;i--)
{ num[i][1]=num[i+1][1]*a+num[i+1][2]*b+1;
for(j=2;j<=i;j++)
num[i][j]=num[i][j-1]*e+num[i+1][j]*c+num[i+1][j+1]*d+1;
}
printf("%.2f\n",num[1][1]);
}
}