现在的位置: 首页 > 综合 > 正文

第36届ACM 北京区域赛邀请赛 F题

2018年04月23日 ⁄ 综合 ⁄ 共 2847字 ⁄ 字号 评论关闭

F  F LOOPS

[Description]

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.

[Input]

The 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

[Output]

A 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

[Hint]

Huge input, "scanf" recommended.


http://acm.bupt.edu.cn/onlinejudge/newoj/showProblem/show_problem.php?problem_id=200

hdu 3853: http://acm.hdu.edu.cn/showproblem.php?pid=3853

话说过了这题之后,我感觉很有必要和期望问题做个了断了,WA了9次,最后重写了一次才过……orz。

这题题意很简单,一个人在左上角的位置,在每个位置上告诉你三个值,分别表示用魔法到达原地、右边位置,下面位置的概率(也就是在用魔法前不知道自己下一步会到哪里),用一次魔法花费2点魔法值,问到达右下角所需要的魔法值的期望。

从终点往起点找,我们可以假设E(i,j)为从终点出发抵达(i,j)位置所需要应用魔法次数的期望(用一次魔法消耗2点魔法值,所以求出用的次数的期望,最后乘以2就可以了)。假设用魔法到达原地,右边位置,下面位置的概率分别为p,q,r,所以会有E(i,j)=pE(i,j)+qE(i,j-1)+rE(i-1,j)+1。这个状态转移方程不能保证p,q,r的和为1,所以开始的解法有问题,如果从后往前推,则状态转移方程为E(i,j)=pE(i,j)+qE(i,j+1)+rE(i+1,j)+1,p,q,r为位置(i,j)给定的三个概率值。

之后只需要在草稿纸上化简之后递推就可以了。

附上代码:

#include <stdio.h>
#include <string.h>

#define eps 1e-5

double map[1005][1005][3];
double dp[1005][1005];


int main()
{
	int i,j,n,m,k,x;
	while (scanf("%d%d",&n,&m)!=EOF)
	{
		for (i=0;i<=n+3;i++)
		{
			for (j=0;j<=m+3;j++)
			{
				dp[i][j]=0;
				map[i][j][0]=map[i][j][1]=map[i][j][2]=0;
			}
		}
		for (i=1;i<=n;i++)
		{
			for (j=1;j<=m;j++)
			{
				for (k=0;k<3;k++)
				{
					scanf("%lf",&map[i][j][k]);
				}
			}
		}
		map[n][m][0]=0;
		for (i=n;i>0;i--)
		{
			for (j=m;j>0;j--)
			{
				if (i==n && j==m) 
				{
					dp[i][j]=0;
					continue;
				}
				x=0;
				for (k=0;k<3;k++)
				{
					if (map[i][j][k]>eps) x++; 
				}
				if ((1-map[i][j][0])<eps) 
				{
					dp[i][j]=0;
					continue;
				}
				dp[i][j]=(((map[i][j][1]*dp[i][j+1]+map[i][j][2]*dp[i+1][j])+1)/(1-map[i][j][0]));
			}
		}
		printf("%.3lf\n",2*dp[1][1]);
	}
	return 0;
}

抱歉!评论已关闭.