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

题意:

你知道,师傅经常被抓,这次又被抓到一个矩阵里面,最开始他在Map[1][1],出口在Map[n][m];每一次他会消耗两颗神丹,然后每一个格子,有一定概率留在原地,有一定概率向下走一格,有一定概率向右走一格。。。求师傅逃出去的神丹消耗期望。

思路:

这次的逃亡很好想,没有前两次那样需要逆推或者求公式。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
double dp[][],a[][][];
int main()
{
int n,m,i,j,k;
while(~scanf("%d%d",&n,&m)){
for(i=;i<=n;i++)
for(j=;j<=m;j++)
for(k=;k<;k++)
scanf("%lf",&a[i][j][k]);
memset(dp,,sizeof(dp));
for(i=n;i>=;i--)
for(j=m;j>=;j--)
{
if(fabs(-a[i][j][])<1e-)//停留原地的概率为1
continue;
dp[i][j]=(dp[i][j+]*a[i][j][]+dp[i+][j]*a[i][j][]+)/(1.0-a[i][j][]);
}
printf("%.3f\n",dp[][]);
}return ;
}

HDU3853LOOPS (师傅逃亡系列•三)(基础概率DP)的更多相关文章

  1. ZOJ3640Help Me Escape(师傅逃亡系列•一)(数学期望||概率DP)

    Background If thou doest well, shalt thou not be accepted? and if thou doest not well, sin lieth at ...

  2. HDU4035 Maze(师傅逃亡系列•二)(循环型 经典的数学期望)

    When wake up, lxhgww find himself in a huge maze. The maze consisted by N rooms and tunnels connecti ...

  3. 【整理】简单的数学期望和概率DP

    数学期望 P=Σ每一种状态*对应的概率. 因为不可能枚举完所有的状态,有时也不可能枚举完,比如抛硬币,有可能一直是正面,etc.在没有接触数学期望时看到数学期望的题可能会觉得很阔怕(因为我高中就是这么 ...

  4. 概率dp小结

    好久之前学过,记得是一次亚洲区的前几天看了看概率dp,然后亚洲区就出了一道概率dp,当时虽然做上了,但是感觉有很多地方没懂,今天起早温习了一下,觉得很多地方茅塞顿开,果然学习的话早上效果最好了. 首先 ...

  5. HDU3853-LOOPS(概率DP求期望)

    LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Su ...

  6. java基础系列(三)---HashMap

    java基础系列(三)---HashMap java基础系列 java基础系列(一)---String.StringBuffer.StringBuilder java基础系列(二)---Integer ...

  7. java基础解析系列(三)---HashMap

    java基础解析系列(三)---HashMap java基础解析系列 java基础解析系列(一)---String.StringBuffer.StringBuilder java基础解析系列(二)-- ...

  8. 【C++自我精讲】基础系列三 重载

    [C++自我精讲]基础系列三 重载 0 前言 分二部分:函数重载,操作符重载. 1 函数重载 函数重载:指在同一名字空间中,函数名称相同,参数类型.顺序或数量不同的一类函数,同一函数名的函数能完成不同 ...

  9. [转]概率DP总结 by kuangbin

    概率类题目一直比较弱,准备把kuangbin大师傅总结的这篇题刷一下! 我把下面的代码换成了自己的代码! 原文地址:http://www.cnblogs.com/kuangbin/archive/20 ...

随机推荐

  1. web视频资料百度云分享

    韩顺平  struts视频教程(31讲)密码:hsp789 链接:https://pan.baidu.com/s/1kSP4CFt1gpVikRaqQXkFGA 密码:kzvp 韩顺平 spring ...

  2. quickSort - 编程细节

    quicksort 快速排序 有几个细节比较重要 今天听到一个清华的大佬的话,他曾经是NOI, 在大一就得到了我梦寐以求的ACM金奖,他这样说, 他们在打NOI的时候,每天要求做10道题, 连续做60 ...

  3. WinCE数据通讯之Web Service篇

    准备写个WinCE平台与数据库服务器数据通讯交互方面的专题文章,今天先整理个Web Service通讯方式. 公司目前的硬件产品平台是WinCE5.0,数据通讯是连接服务器与终端的桥梁,关系着终端的数 ...

  4. Codeforces Round #424

    基本全是水题 第一题水,不过有hack点,先增后不变再减 #include<map> #include<set> #include<cmath> #include& ...

  5. 总结网站Mysql优化

    Mysql存储引擎 选择合适的存储引擎Innodb  myisam myisam: 写入数据非常快,适合使用场合dedecms/phpcms/discuz/微博系统等写入.读取操作多的系统. inno ...

  6. R读取一个数据框 Dataframe,删去其中的某一列

    可以参考:http://blog.sina.com.cn/s/blog_80572f5d0101anxw.html

  7. opencv 图片降噪

    —— # -*- coding: utf-8 -* import numpy as np import cv2 cap = cv2.VideoCapture(0) while True: _ , fr ...

  8. 【MySQL】Error 1264: out of range value for column

    此问题是插入的整型数字超出了范围. 比如设置表格的数据类型:cust_fax integer(10) not null 当插入以下数字的时候会抛出标题所说的错误: insert into databa ...

  9. Sum All Numbers in a Range

    我们会传递给你一个包含两个数字的数组.返回这两个数字和它们之间所有数字的和. 最小的数字并非总在最前面. 这是一些对你有帮助的资源: Math.max() Math.min() Array.reduc ...

  10. wxpython的简单的应用

    import wximport wx.xrc import pandas as pd from conf.env import * # 允许选择的文件wildcard = "Python s ...