LOOPS

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Submission(s): 2552    Accepted Submission(s): 1041

Problem 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
 
Source
 
题意:有一个r*c的格子,从格子(1,1)出发,可以保持三种姿势,留在原地,向右一步,向下一步,且给出相应的概率,每走一步消耗2的魔法值,问最后到达(r,c)平均需要多少魔法值、
 
题解:
    我们知道概率的  E(i)=p1+p2+p3......+pn;
    对于E(ax+bk)=a*Ex+b*Ek;
我们不妨从(r,c)出发:
        首先要明确的是: 在没有到达(r,c)之前,是不能停留的。所以
         dp[1][1]=dp[1][2]*mat[1][1][3]+dp[2][1]*mat[1][1][2];
         dp[1][1]=dp[1][1]/(1-map[1][1][0])   //除去停下来的其他概率,得到期望    
依次这样推:
        dp[i][j]=dp[i][j+1]*mat[i][j][3]+dp[i+1][j]*mat[i][j][2];

         dp[i][j]=dp[i][j]/(1-map[i][j][0])   //除去停下来的其他概率,得到期望  
但是由于这样从(1,1)推断,会出现将不收敛问题;
代码:
//#define LOCAL
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxn 1001
double dp[maxn][maxn];
double map[maxn][maxn][]; int main()
{
int rr,cc;
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif while(scanf("%d%d",&rr,&cc)!=EOF)
{
for(int i=;i<=rr;i++)
for(int j=;j<=cc;j++)
scanf("%lf%lf%lf",&map[i][j][],&map[i][j][],&map[i][j][]);
memset(dp,,sizeof(dp));
for(int i=rr;i>;i--)
for(int j=cc;j>;j--){
if(i==rr&&j==cc)
continue; //(rr,cc)这个点是出口不需要在走了,停在原地 if(map[i][j][]!=1.0) //如果没有到终点停下来的话,题目就会误解!
{
dp[i][j]=dp[i][j]*map[i][j][]+dp[i+][j]*map[i][j][]+dp[i][j+]*map[i][j][]+;
dp[i][j]/=(1.0-map[i][j][]);
} }
printf("%.3lf\n",dp[][]);
} return ;
}

hdu 3853LOOPS (概率DP)的更多相关文章

  1. HDU 4599 概率DP

    先推出F(n)的公式: 设dp[i]为已经投出连续i个相同的点数平均还要都多少次才能到达目标状态. 则有递推式dp[i] = 1/6*(1+dp[i+1]) + 5/6*(1+dp[1]).考虑当前这 ...

  2. HDU 5001 概率DP || 记忆化搜索

    2014 ACM/ICPC Asia Regional Anshan Online 给N个点,M条边组成的图,每一步能够从一个点走到相邻任一点,概率同样,问D步后没走到过每一个点的概率 概率DP  測 ...

  3. hdu 3853 概率dp

    题意:在一个R*C的迷宫里,一个人在最左上角,出口在右下角,在每个格子上,该人有几率向下,向右或者不动,求到出口的期望 现在对概率dp有了更清楚的认识了 设dp[i][j]表示(i,j)到(R,C)需 ...

  4. HDU 4815 概率dp,背包

    Little Tiger vs. Deep Monkey Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K ( ...

  5. hdu 4050(概率dp)

    算是挺简单的一道概率dp了,如果做了前面的聪聪于可可的话,这题不需要什么预处理,直接概率dp就行了... #include <stdio.h> #include <stdlib.h& ...

  6. HDU 4405 (概率DP)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4405 题目大意:飞行棋.如果格子不是飞行点,扔骰子前进.否则直接飞到目标点.每个格子是唯一的飞行起点 ...

  7. hdu 4336 概率dp + 状压

    hdu 4336 小吃包装袋里面有随机赠送一些有趣的卡片,如今你想收集齐 N 张卡片.每张卡片在食品包装袋里出现的概率是p[i] ( Σp[i] <= 1 ), 问你收集全部卡片所需购买的食品数 ...

  8. hdu 4576(概率dp+滚动数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4576 思路:由于每次从某一位置到达另一位置的概率为0.5,因此我们用dp[i][j]表示第i次操作落在 ...

  9. hdu 5001 概率DP 图上的DP

    http://acm.hdu.edu.cn/showproblem.php?pid=5001 当时一看是图上的就跪了 不敢写,也没退出来DP方程 感觉区域赛的题  一则有一个点难以想到 二则就是编码有 ...

随机推荐

  1. CUBRID学习笔记 3 net连接数据库并使用cubrid教程示例

    接上文 数据库安装好后,也可以测试语句了. 下面我们用c#写一个控制台程序,连接数据库,并读取数据. 一 下载驱动  net版的下 CUBRID ADO.NET Data Provider 9.3.0 ...

  2. 第五章 consul key/value

    1.key/value作用 动态修改配置文件 支持服务协同 建立leader选举 提供服务发现 集成健康检查 2.使用 2.1.查看全部key/value 说明: 使用?recurse参数来指定查看多 ...

  3. Linux红帽认证----I Want

    仅此设定一个目标,此证一定搞到手!!!

  4. Fiddler中session的请求/响应类型与图标对照表

  5. IBM Lotus Domino V8.5 服务器管理入门手册

    转自 http://freemanluo.blog.51cto.com/636588/336128

  6. 使用Maven构件Web应用

    一个典型的WAR文件会有如下目录结构: Maven的WEB项目结构: jetty-maven-plugin默认很好地支持了Maven的项目结构.在通常情况下,我们只需要直接在IDE中修改源码,IDE能 ...

  7. javascript学习-原生javascript的小特效(多个运动效果整理)

    以下代码就不详细解析了,在我之前的多个运动效果中已经解析好多次了,重复的地方这里就不说明了,有兴趣的童鞋可以去看看之前的文章<原生javascript的小特效> <!DOCTYPE ...

  8. bootstrap学习笔记<六>(表单二之按钮)

    按钮(补充) (ps:居中元素可以使用<center></center>标签) 块级按钮(ps:按钮占一整行) <button class="btn btn-p ...

  9. poj2354Titanic(两点的球面距离)

    链接 球面距离计算公式:d(x1,y1,x2,y2)=r*arccos(sin(x1)*sin(x2)+cos(x1)*cos(x2)*cos(y1-y2)) x1,y1是纬度\经度的弧度单位,r为地 ...

  10. Python的lambda表达式

    使用lambda来创建匿名函数,而用def创建的方法是有名称的,除了从表面上的方法名不一样外,python lambda还有哪些和def不一样呢? 1 python lambda会创建一个函数对象,但 ...