期望DP——C - LOOPS HDU - 3853

博客目录 

返回二级目录——kaugnbin概率dp习题集

一、原题

原题传送门

Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). 

Homura wants to help her friend Madoka save the world. But because of the plot of the Boss Incubator, she is trapped in a labyrinth called LOOPS. 


The planform of the LOOPS is a rectangle of R*C grids. There is a portal in each grid except the exit grid. It costs Homura 2 magic power to use a portal once. The portal in a grid G(r, c) will send Homura to the grid below G (grid(r+1, c)), the grid on the right of G (grid(r, c+1)), or even G itself at respective probability (How evil the Boss Incubator is)! 
At the beginning Homura is in the top left corner of the LOOPS ((1, 1)), and the exit of the labyrinth is in the bottom right corner ((R, C)). Given the probability of transmissions of each portal, your task is help poor Homura calculate the EXPECT magic power she need to escape from the LOOPS. 


 

InputThe first line contains two integers R and C (2 <= R, C <= 1000). 

The following R lines, each contains C*3 real numbers, at 2 decimal places. Every three numbers make a group. The first, second and third number of the cth group of line r represent the probability of transportation to grid (r, c), grid (r, c+1), grid (r+1, c) of the portal in grid (r, c) respectively. Two groups of numbers are separated by 4 spaces. 

It is ensured that the sum of three numbers in each group is 1, and the second numbers of the rightmost groups are 0 (as there are no grids on the right of them) while the third numbers of the downmost groups are 0 (as there are no grids below them). 

You may ignore the last three numbers of the input data. They are printed just for looking neat. 

The answer is ensured no greater than 1000000. 

Terminal at EOF 


OutputA real number at 3 decimal places (round to), representing the expect magic power Homura need to escape from the LOOPS. 

Sample Input

2 2
0.00 0.50 0.50    0.50 0.00 0.50
0.50 0.50 0.00    1.00 0.00 0.00

Sample Output

6.000

 

二、题目大意

一个迷宫,起点是(1,1),终点是(R,C),迷宫的每个格子都有一个传送门,可以花费2个单位的魔力值的随机传送:

1.传送到右边的格子(r+1, c)

2.传送到下边的格子(r, c+1)

3.传送到原来的格子(即只浪费魔力不传送)

求到达终点所需的魔力值的期望。

撤销一步的花费为0,即如果进入了死循环的格子,可以退回且不花费多余的魔力值

(边界传送门不会传送越界,仔细读input)

三、分析

 

  • 期望从后往前推,概率从前往后推;
  • 方程分析

设dp[i][j]为从(i,j)到达终点所需的魔力值(期望)

用e表示dp[i][j],用ea表示dp[i+1][j],用eb=dp[i][j+1]

根据期望全概率公式(公式名字应该没记错):又:由门的每种传送情况概率为p1,p2,p

e=p1*ea+p2*eb+p*e+2  

即从(i,j)到达终点的期望等于   ∑(下一种向转移情况所需的期望*每种转移情况对应的概率)+2(这个2是这一步的花费)

解方程,将e移到方程的左边

(1-p)*e=p1*ea+p2*eb+2

e=p1/(1-p)*ea+p2/(1-p)*eb+2/(1-p)    (应该没推错,好久之前写的题)

注意p=1的情况(p代表的是转移到自己的概率)

当p=1时表示传送门只能转移到自己,即撤销一步(花费为0),所以可以令e=0(自己悉心体会一下)

接下来只需要写个递推式就好了。

 

  • 边界条件:dp置0
  • 结果

四、AC代码

#include<bits/stdc++.h>
using namespace std;
int const maxn=1e3+10;
typedef struct node
{
	double s,r,d;
}node;
node mp[maxn][maxn];
double dp[maxn][maxn];
int main()
{
	int h,w;
	while(cin>>h>>w)
	{
		int i,j;
		for(i=0;i<h;i++)
		{
			for(j=0;j<w;j++)
			{
				scanf("%lf%lf%lf",&mp[i][j].s,&mp[i][j].r,&mp[i][j].d);
			}
		}
		memset(dp,0,sizeof dp);
		for(i=h-1;i>=0;i--)
		{
			for(j=w-1;j>=0;j--)
			{
				if(1-mp[i][j].s>1e-5)
					dp[i][j]=
						(mp[i][j].r*dp[i][j+1]+mp[i][j].d*dp[i+1][j]+2)/(1-mp[i][j].s);
				else
					dp[i][j]=0;
			}
		}
		printf("%.3lf\n",dp[0][0]);
	}
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值