Problem 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
Sample Input
3 0.3 0.7 0.1 0.3 0.6 0
Sample Output
3.41
阶梯型迷宫。
从1,1到n,1的概率。
每个房间最多有三个们。左,左下,右下。
如果只有左门,则概率为1.
只有左下右下,a,b
同时有三个,c,d,e。
求概率。
反向思考,我们从(n,1)往(1,1)走。
我们的数组是这样列的,一定要注意
(1,1)
(2,1)(2,2)
(3,1)(3,2)(3,3)
下面的三个步骤的走法不要搞糊涂了。我们左下角是n,1.
①对于第n行的房间我们怎么走。
对于(n,i),我们只能从(n,i-1)到达,也就是概率为1.
②对于第1列的房间(i,1)
我们只有两种进入方法,从(i+1,2)(i,2)到达。
③对于其它的(i,j)。
我们可以从(i+1,j)(i+1,j+1)(i,j-1)到达。
所以利用dp从n行到1行,从1列到i列枚举即可。
#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
double mp[110][110];
double a,b,c,d,e;
int n;
int main()
{
while(cin>>n&&n!=0)
{
memset(mp,0,sizeof(mp));
cin>>a>>b>>c>>d>>e;
for(int i=n;i>=0;i--)
{
for(int j=1;j<=i;j++)
{
if(i==n&&j!=1)//只能往左走的,概率为1.也就是最后一行,我们只能往左
{
mp[i][j]=mp[i][j-1]+1;
}
if(j==1&&i!=n)//第一列的可以由左下和右下到达
{
mp[i][j]=a*(mp[i+1][j]+1)+b*(mp[i+1][j+1]+1);
}
if(i!=n&&j!=1)//其他位置的点,都有三种到达方式
{
mp[i][j]=d*(mp[i+1][j+1]+1)+e*(mp[i][j-1]+1)+c*(mp[i+1][j]+1);
}
}
}
printf("%0.2f\n",mp[1][1]);
}
}