问题:

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

题意:有一个R*C的网格,每走一次消耗2点能量,现在要从起点 (1,1) 走到 (R , C),有三种走法:第一种原点不动;第二种向下走;第三种:向右走。三种走法的概率之和为1。求走到出口需要能量值的期望值。

思路:dp期望值的入门题,直接套数学期望值的公式即可,代码有详解。

代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define ll long long
#define PI acos(-1.0)
#define eps 1e-8
#define mod 1000000007
#define lowbit(x) x&(-x)
#define N 1010
//原点的概率,往右走一步的概率,往下走一步的概率分别用p1,p2,p3表示,
//则期望等于dp[i][j]=dp[i][j]*p1+dp[i][j+1]*p2+dp[i+1][j]*p3+2(2为移动一次的消耗的能量)化简为
//dp[i][j]=(dp[i][j+1]*p2+dp[i+1][j]*p3+2)/(1-p1);
double dp[N][N];
double s[N][N][3];//表示原点,往右走一步,往下走一步
double a,b,c,d;
int n,m;
int main()
{while(~scanf("%d%d",&n,&m)){mem(dp,0.0);for(int i=1; i<=n; i++)for(int j=1; j<=m; j++)for(int k=0; k<3; k++)scanf("%lf",&s[i][j][k]);//这道题比较简单,不用再去算概率了for(int i=n; i>0; i--)for(int j=m; j>0; j--){a=1.0*s[i][j][0];//原点的概率b=1.0*dp[i][j+1]*s[i][j][1];//往右走一步的期望c=1.0*dp[i+1][j]*s[i][j][2];//往下走一步的期望if(i==n&&j==m)continue;if(1.0-a<eps)continue;//这一步如果1.0-a==0,是不能进行下一步的,分母不能为零dp[i][j]=(b+c+2.0)/(1.0-a);}printf("%.3f\n",dp[1][1]);}
}

LOOPS HDU - 3853 dp求期望值相关推荐

  1. LOOPS HDU - 3853 (概率dp):(希望通过该文章梳理自己的式子推导)

    题意:就是让你从(1,1)走到(r, c)而且每走一格要花2的能量,有三种走法:1,停住.2,向下走一格.3,向右走一格.问在一个网格中所花的期望值. 首先:先把推导动态规划的基本步骤给出来. · 1 ...

  2. LOOPS HDU - 3853(概率dp,期望)

    题意: 有一个R*C的方格.一个人想从(1,1)走到(r,c).在每个格子都有三种选择,向下,向右,或者原地不动.每个格子里的每个选择都有一定的概率.而每次移动都需要消耗2点的能量,问期望消耗的能量是 ...

  3. HDU 4832(DP+计数问题)

    HDU 4832 Chess 思路:把行列的情况分别dp求出来,然后枚举行用几行.竖用几行,然后相乘累加起来就是答案 代码: #include <stdio.h> #include < ...

  4. HDU 5928 DP 凸包graham

    给出点集,和不大于L长的绳子,问能包裹住的最多点数. 考虑每个点都作为左下角的起点跑一遍极角序求凸包,求的过程中用DP记录当前以j为当前末端为结束的的最小长度,其中一维作为背包的是凸包内侧点的数量.也 ...

  5. hdu 1171 dp(多重背包)

    View Code //hdu 1171 dp(多重背包)//题意:把所有物品的价值尽量分为相等的两份,不能等分的话 //后面那份可以稍小于前面的 //求出价值总和后,令价值的一半为背包容量,让背包尽 ...

  6. 2019杭电多校 第七场 Kejin Player 6656(求期望值)

    2019杭电多校 第七场 Kejin Player 6656(求期望值) 题目 http://acm.hdu.edu.cn/showproblem.php?pid=6656 题意 给你n,q.表示有n ...

  7. 旅游(树形dp求树的最大独立集)

    链接:https://ac.nowcoder.com/acm/problem/15748 来源:牛客网 题目描述 Cwbc和XHRlyb生活在s市,这天他们打算一起出去旅游. 旅行地图上有n个城市,它 ...

  8. 树上子链(树形dp求树的直径)

    树上子链 题意: 给定一棵树 T ,树 T 上每个点都有一个权值. 定义一颗树的子链的大小为:这个子链上所有结点的权值和 . 请在树 T 中找出一条最大的子链并输出. 题解: 求树的直径,题目中存在负 ...

  9. Check the difficulty of problems (概率dp求概率)

    Check the difficulty of problems POJ - 2151 大致题意: m个问题,t个队伍,要求冠军队伍至少解决n个问题,给出每个队伍解决每个问题的概率 求每一个队至少解决 ...

最新文章

  1. 【Qt】QImage加载bmp位图数据
  2. 推荐系统发展的六大影响因子 | 深度
  3. NLP顶级专家Dan Roth :自然语言处理领域近期的任务和主要应用
  4. IBM首家发布了公有云中的裸机Kubernetes
  5. 码栈开发手册(三)---编码方式开发(高级课程①)
  6. 华为鸿蒙净水机,华为鸿蒙OS 2.0手机版功能抢先曝光
  7. unity 关闭自己脚本_太可了!这些领域可以把Unity玩的那么好,带你开启新世界...
  8. 清华大学迎来中国首位原创AI虚拟学生;百度造车:定价20万元以上;亚马逊面临 7.5 万项仲裁|极客头条...
  9. php 放大镜代码,jQuery实现放大镜效果实例代码_jquery
  10. 集成显卡连接显示器的线跟独立显卡的不同么,分别叫什么
  11. API接口出来了,填写客户收货地址不用烦,自动填写不出差错
  12. 怎样用计算机10,win10计算器怎么用_win10如何打开计算器
  13. CGAL预定义内核转换的问题
  14. 惠普e240计算机开机,HP EliteDisplay E240 23.8 英寸显示器(符合能源之星标准)
  15. 初学者-CSS思维导图(下)
  16. 如何实现一款好用的云图(word-cloud)生成工具?
  17. 关于ElasticSearch (ES)
  18. Excel切片器的使用
  19. oracle 退出代码 255,sql – 奇怪的SSIS错误(进程退出代码255)
  20. android 优秀的开源框架整理

热门文章

  1. solar2 android,solarwalk2
  2. 什么叫真正的IT男?
  3. 2021-DASCTF-三月赛-Writeup
  4. 开源权限引擎可能理解了骇客帝国
  5. PostgreSQL 查询所有表的记录数
  6. 关于关联表用code还是id的见解
  7. 冯·诺依曼计算机与现代计算机原理与差别
  8. 傅里叶级数展开及系数项求解
  9. Python----稀疏矩阵
  10. JavaWeb基础04: JavaScript