The number of steps(概率dp问题)

本文探讨了一个迷宫寻钥问题,利用概率动态规划(DP)算法求解从迷宫顶部到最底层左端房间所需平均步数。通过设定不同条件下的转移概率,实现了对每一步可能路径的精确计算。
题目描述

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.

样例输入
3
0.3 0.7
0.1 0.3 0.6
0 
样例输出
3.41
思路

概率dp问题
dp [ i ] [ j ] 代表第 i 行第 j 个数所需要的步数
转移方程:
第n行: dp [ n ] [ i ] = dp [ n ] [ i - 1 ] + 1;
其他行:
如果位于左边界 dp [ i ] [ j ] = dp [ i + 1 ] [ j ] * a + dp [ i + 1 ] [ j + 1 ] * b + 1;
否则 dp [ i ] [ j ] = dp [ i + 1 ] [ j ] * c + dp [ i + 1 ] [ j + 1 ] * d + dp [ i ] [ j - 1 ] * e + 1;
(这是我做的第一个关于概率dp 的问题吧,拿来纪念一下( * ^ ▽ ^ * ))

代码
#include<iostream>
#include<string>
#include<map>
#include<set>
//#include<unordered_map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<stack>
#include<cmath>
#include<fstream>
#define X first
#define Y second
#define best 131 
#define INF 0x3f3f3f3f
#define ls p<<1
#define rs p<<1|1
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double eps=1e-5;
const double pai=acos(-1.0);
const int N=1e5;
const int maxn=1e6+10;
double dp[50][50];
int main( )
{
    int n;
    double a,b,c,d,e;
    while(~scanf("%d",&n))
    {
        if(n==0) break;
        scanf("%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e);
        dp[n][1]=0;
        for(int i=2;i<=n;i++)
        {
            dp[n][i]=dp[n][i-1]+1;
        }
        for(int i=n-1;i>=1;i--)
        {
            for(int j=1;j<=i;j++)
            {
                if(j==1) dp[i][j]=dp[i+1][j]*a+dp[i+1][j+1]*b+1;
                else dp[i][j]=dp[i+1][j]*c+dp[i+1][j+1]*d+dp[i][j-1]*e+1;
            }
        }
        printf("%.2f\n",dp[1][1]);
    }
    return 0;
}
Unlike English, Chinese words are not seperated by space. It's hard for computer to divide a sentence into words correctly. However, you can manage it by doing these steps. 1.Analyis a large number of articles, and count all the words and their ocurrence frequency. 2.Find a way to seperate the sentence into words, so that the probability product of all the words is largest. That means a sentence s is divided into w1/w2/w3.../wn, because p(w1)*p(w2)*p(w3)...*p(wn) is larger than any other p(v1)*p(v2)...p(vm). If a sentence is nihaoshijie, the best solution is nihao/shijie ,because p(nihao) * p(shijie) is largest of all solutions. You are given the words and their frequency. Do the division. The First part is the words and frequency. Every line is a wordi, string contains only lower case characters and a integer f(wordi). To simplify the problem, f(wordi) is not the real frequency. You can think the frequency is p(wordi), f(wordi) = (int)(log(p(wordi)). So the sum of fs should be largest, and all fs shouldn't be 0. The length of word is less than 50. The number of words is less than 40000. All the integers can be handled with int. The first part is ended with "#END". The sencond part is the sentences to be divided. Each line is a string contains only lower case characters. The length of the line is less than 5000. There are T sentences to be divided. (T<5) Each sentence a line. Print the line with "/" to seperate the words.The solution is unique. Print a blank line after each sentence. 输入样例 an 1 chu 1 chuan 1 huan 1 hu 1 hua 1 chuang 5 qi 1 an 1 qian 5 mi 1 min 1 ming 5 yu 1 yue 5 e 1 gu 1 gua 1 guan 1 guang 5 #END chuangqianmingyueguang 输出样例 chuang/qian/ming/yue/guang
最新发布
12-24
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值