HDU 3853 LOOP (概率DP求期望)
Description
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 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
Sample Input
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
题意:
有一个迷宫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)所需要消耗的魔力的期望值。
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
double f[][];
double p1[][],p2[][],p3[][];
int main()
{
int n,s;
while(scanf("%d%d",&n,&s)!=EOF)
{
for(int i=;i<=n;i++)
for(int j=;j<=s;j++)
scanf("%lf%lf%lf",&p1[i][j],&p2[i][j],&p3[i][j]);
memset(f,,sizeof(f));
for(int i=n;i>=;i--)
{
for(int j=s;j>=;j--)
{
if(i==n&&j==s)
continue;
if(p1[i][j]==1.00) //分母为0
continue;
f[i][j]=p2[i][j]*f[i][j+]+p3[i][j]*f[i+][j]+2.0;
f[i][j]/=(-p1[i][j]);
}
}
printf("%.3f\n",f[][]); //是f[1][1],不是f[0][0]。
}
return ;
}
HDU 3853 LOOP (概率DP求期望)的更多相关文章
- HDU3853-LOOPS(概率DP求期望)
LOOPS Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others) Total Su ...
- POJ2096 Collecting Bugs(概率DP,求期望)
Collecting Bugs Ivan is fond of collecting. Unlike other people who collect post stamps, coins or ot ...
- LightOJ 1030 【概率DP求期望】
借鉴自:https://www.cnblogs.com/keyboarder-zsq/p/6216762.html 题意:n个格子,每个格子有一个值.从1开始,每次扔6个面的骰子,扔出几点就往前几步, ...
- HDU 5245 Joyful(概率题求期望)
D - Joyful Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit S ...
- hdu 3853 LOOPS 概率DP
简单的概率DP入门题 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include ...
- HDU 3853 LOOPS 概率DP入门
LOOPS Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)Total Sub ...
- hdu 3853 LOOPS (概率dp 逆推求期望)
题目链接 LOOPS Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)Tota ...
- HDU 4405 Aeroplane chess (概率DP求期望)
题意:有一个n个点的飞行棋,问从0点掷骰子(1~6)走到n点须要步数的期望 当中有m个跳跃a,b表示走到a点能够直接跳到b点. dp[ i ]表示从i点走到n点的期望,在正常情况下i点能够到走到i+1 ...
- hdu 4405 Aeroplane chess(简单概率dp 求期望)
Aeroplane chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
随机推荐
- php结合redis实现高并发下的抢购、秒杀功能【转】
抢购.秒杀是如今很常见的一个应用场景,主要需要解决的问题有两个:1 高并发对数据库产生的压力2 竞争状态下如何解决库存的正确减少("超卖"问题)对于第一个问题,已经很容易想到用缓存 ...
- JavaSE 第二次学习随笔(作业一)
package homework2; import java.io.ObjectInputStream.GetField; import java.util.Arrays; public class ...
- 学习python第十五天,面向对象
Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的. 面向对象技术简介 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集 ...
- 笔记-DB-mongodb-常用操作-1
笔记-DB-mongodb-常用操作-1 1. 启动及连接 1.1. 启动 启动mongod windows下: 1. 如已添加服务 net start <service name> ...
- 搭建Linpack
环境:vmware workstation14 + centos7(linux基本都可以) 一.开始安装mpich 1. 解决依赖gcc gcc-gfortran sudo yum install g ...
- 大话卷积神经网络(CNN)
这几年深度学习快速发展,在图像识别.语音识别.物体识别等各种场景上取得了巨大的成功,例如AlphaGo击败世界围棋冠军,iPhone X内置了人脸识别解锁功能等等,很多AI产品在世界上引起了很大的 ...
- format内置函数
1. 函数功能将一个数值进行格式化显示. 2. 如果参数format_spec未提供,则和调用str(value)效果相同,转换成字符串格式化. >>> format(3.14159 ...
- myeclipse中项目名有红叉,但项目中文件没有报错的解决办法
导入了别人的项目,各种jar包都放好后,path也都build好了,项目也能正常启动,但是就是项目名有红叉,这是为什么呢? 网上有人说Java build path中的jar包missing了,这是一 ...
- day06_07 字典操作02
1.0 删_del dic5 = {'age':18,'name':'alex','hobby':'girl'} del dic5['name'] #删除键值对 print(dic5) #>&g ...
- ImportError: libcudart.so.8.0: cannot open shared object file: No such file or directory
这是在python中使用caffe产生的错误. 程序很普通: #-*-coding=utf-8-*- import numpy as npimport matplotlib.pyplot as plt ...