D - LOOPS

Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

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
和上一篇博客POJ 2096是一个意思。
 

题意:
有一个迷宫r行c列,开始点在[1,1]现在要走到[r,c]
对于在点[x,y]可以打开一扇门走到[x,y]或者[x+1,y]或者[x,y+1]
消耗2点魔力 问平均消耗多少魔力能走到[r,c]

分析:

输入r和c 随后r行c列 输入三个概率
假设dp[i][j]表示在点[i,j]到达[r,c]所需要消耗的平均魔力(期望)
则从dp[i][j]可以到达:
dp[i][j],dp[i+1,j],dp[i][j+1];
对应概率分别为: p1[i][j],p2[i][j],p3 [i][j]
由E(aA+bB+cC...)=aEA+bEB+cEC+...//包含状态A,B,C的期望可以分解子期望求解
得到dp[i][j]=p1[i][j]*dp[i][j]+p2[i][j]*dp[i+1][j]+p3[i][j]*dp[i][j+1]+2;

得出最终公式:dp[i][j]]=(p2[i][j]*dp[i+1][j]+p3[i][j]*dp[i][j+1]+2)/(1-p1[i][j])

注意分母为0的时候要特判一下

dp[i][j]表示从(i,j)走到(n,s)所需要消耗的魔力的期望值。

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. using namespace std;
  5. double f[][];
  6. double p1[][],p2[][],p3[][];
  7. int main()
  8. {
  9. int n,s;
  10. while(scanf("%d%d",&n,&s)!=EOF)
  11. {
  12. for(int i=;i<=n;i++)
  13. for(int j=;j<=s;j++)
  14. scanf("%lf%lf%lf",&p1[i][j],&p2[i][j],&p3[i][j]);
  15. memset(f,,sizeof(f));
  16. for(int i=n;i>=;i--)
  17. {
  18. for(int j=s;j>=;j--)
  19. {
  20. if(i==n&&j==s)
  21. continue;
  22. if(p1[i][j]==1.00) //分母为0
  23. continue;
  24. f[i][j]=p2[i][j]*f[i][j+]+p3[i][j]*f[i+][j]+2.0;
  25. f[i][j]/=(-p1[i][j]);
  26. }
  27. }
  28. printf("%.3f\n",f[][]); //是f[1][1],不是f[0][0]。
  29. }
  30. return ;
  31. }

HDU 3853 LOOP (概率DP求期望)的更多相关文章

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

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

  2. POJ2096 Collecting Bugs(概率DP,求期望)

    Collecting Bugs Ivan is fond of collecting. Unlike other people who collect post stamps, coins or ot ...

  3. LightOJ 1030 【概率DP求期望】

    借鉴自:https://www.cnblogs.com/keyboarder-zsq/p/6216762.html 题意:n个格子,每个格子有一个值.从1开始,每次扔6个面的骰子,扔出几点就往前几步, ...

  4. HDU 5245 Joyful(概率题求期望)

    D - Joyful Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit S ...

  5. hdu 3853 LOOPS 概率DP

    简单的概率DP入门题 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include ...

  6. HDU 3853 LOOPS 概率DP入门

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

  7. hdu 3853 LOOPS (概率dp 逆推求期望)

    题目链接 LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)Tota ...

  8. HDU 4405 Aeroplane chess (概率DP求期望)

    题意:有一个n个点的飞行棋,问从0点掷骰子(1~6)走到n点须要步数的期望 当中有m个跳跃a,b表示走到a点能够直接跳到b点. dp[ i ]表示从i点走到n点的期望,在正常情况下i点能够到走到i+1 ...

  9. hdu 4405 Aeroplane chess(简单概率dp 求期望)

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

随机推荐

  1. DSP资源分享贴

    DSP资源分享 [2017.5.16 更新] 分享资源共同学习.以前的资源很多人都说用不了了,我会陆续补充,逐步完善.这里不单单分享DSP的,设计基础的,还有其他的电子相关的比较好的资源吧主都和您分享 ...

  2. 呕心沥血写的python猜数字

    #猜数字 import random num_rd=random.randint(0,100) count=1 while 1<=count<=10: num_ip=input('请输入0 ...

  3. gcc常用语法

    1. gcc -E source_file.c-E,只执行到预编译.直接输出预编译结果. 2. gcc -S source_file.c -S,只执行到源代码到汇编代码的转换,输出汇编代码. 3. g ...

  4. python——获取数据类型

    在python中,可使用type()和isinstance()内置函数获取数据类型 如: (1)type()的使用方法: >>> a = '230'         >> ...

  5. Python的类(一)

    类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 类变量:类变量在整个实例化的对象中是公用的.类变量定义在类中且在函数体之外. ...

  6. ADMX Migrator

    实用工具特别推荐ADMX MigratorLance Whitney 下载这篇文章的代码: ADMX Migrator (2765KB) 对于那些 使用组策略的人而言,他们自然非常熟悉如何使用管理模板 ...

  7. 《Cracking the Coding Interview》——第16章:线程与锁——题目3

    2014-04-27 19:26 题目:哲学家吃饭问题,死锁问题经典模型(专门用来黑哲学家的?). 解法:死锁四条件:1. 资源互斥.2. 请求保持.3. 非抢占.4. 循环等待.所以,某砖家拿起一只 ...

  8. 《Cracking the Coding Interview》——第4章:树和图——题目9

    2014-03-19 05:07 题目:给定一棵二叉树T和一个值value,在T中找出所有加起来和等于value的路径.路径的起点和终点都可以是树的任意节点. 解法:我偷了个懒,直接把这棵树看成一个无 ...

  9. Django笔记 —— 入门简介

    最近在学习Django,打算玩玩网页后台方面的东西,因为一直很好奇但却没怎么接触过.Django对我来说是一个全新的内容,思路想来也是全新的,或许并不能写得很明白,所以大家就凑合着看吧- 本篇笔记(其 ...

  10. ajax向Asp.NET后端传递数组型数据

    近日,在开发一个组件的过程中,需要通过Ajax对象向Asp.NET后端传递一个比较复杂的表单,表单中的一个字段是数组类型,我能想到的办法是用JSON.stringify将前端的数组对象序列化成字符串, ...