期望DP

期望DP的一般做法是从末状态開始递推:

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

dp[i][j]表示点(i,j)到达(r,c)的期望步数:

特殊处理呆在原地概率为1的点。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
const int maxn=1000+100;
double dp[maxn][maxn];
double p[maxn][maxn][3];
int r,c;int main()
{while(~scanf("%d%d",&r,&c)){for(int i=1;i<=r;i++){for(int j=1;j<=c;j++)scanf("%lf%lf%lf",&p[i][j][0],&p[i][j][1],&p[i][j][2]);}memset(dp,0,sizeof(dp));for(int i=r;i>=1;i--){for(int j=c;j>=1;j--){if(p[i][j][0]==1)  continue;else  dp[i][j]=(dp[i][j+1]*p[i][j][1]+dp[i+1][j]*p[i][j][2]+2)/(1-p[i][j][0]);}}printf("%.3f\n",dp[1][1]);}return 0;
}


 
 UPC  2225: The number of steps 

Description

Mary stands in a strange maze, the maze looks like a triangle(the first layer have one room,the second layer have two rooms,the third layer have three rooms …). Now she stands at the top point(the first layer), and the KEY of this maze is in the lowest layer’s leftmost room. Known that each room can only access to its left room and lower left and lower right rooms .If a room doesn’t have its left room, the probability of going to the lower left room and lower right room are a and b (a + b = 1 ). If a room only has it’s left room, the probability of going to the room is 1. If a room has its lower left, lower right rooms and its left room, the probability of going to each room are c, d, e (c + d + e = 1). Now , Mary wants to know how many steps she needs to reach the KEY. Dear friend, can you tell Mary the expected number of steps required to reach the KEY?

 

Input

There are no more than 70 test cases. 
In each case , first Input a positive integer n(0<n<45), which means the layer of the maze, then Input five real number a, b, c, d, e. (0<=a,b,c,d,e<=1, a+b=1, c+d+e=1). 
The input is terminated with 0. This test case is not to be processed.

Output

Please calculate the expected number of steps required to reach the KEY room, there are 2 digits after the decimal point.

Sample Input

30.3 0.70.1 0.3 0.60 

Sample Output

3.41
#include<iostream> 
#include<cstdio> 
#include<cstring> 
#include<algorithm> 
#include<limits.h> 
typedef long long LL; 
using namespace std; 
double a,b,c,d,e; 
double dp[50][50]; 
int main() 
{ int n; while(~scanf("%d",&n)&&n) { scanf("%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e); for(int i=1;i<=n;i++) dp[n][i]=i-1; for(int i=n-1;i>=1;i--) { dp[i][1]=dp[i+1][1]*a+dp[i+1][2]*b+1; for(int j=2;j<=i;j++) dp[i][j]=dp[i+1][j]*c+dp[i+1][j+1]*d+dp[i][j-1]*e+1; } printf("%.2f\n",dp[1][1]); } return 0; 
} 
POJ 2096

Description

Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other material stuff, he collects software bugs. When Ivan gets a new program, he classifies all possible bugs into n categories. Each day he discovers exactly one bug in the program and adds information about it and its category into a spreadsheet. When he finds bugs in all bug categories, he calls the program disgusting, publishes this spreadsheet on his home page, and forgets completely about the program. 
Two companies, Macrosoft and Microhard are in tight competition. Microhard wants to decrease sales of one Macrosoft program. They hire Ivan to prove that the program in question is disgusting. However, Ivan has a complicated problem. This new program has s subcomponents, and finding bugs of all types in each subcomponent would take too long before the target could be reached. So Ivan and Microhard agreed to use a simpler criteria --- Ivan should find at least one bug in each subsystem and at least one bug of each category. 
Macrosoft knows about these plans and it wants to estimate the time that is required for Ivan to call its program disgusting. It's important because the company releases a new version soon, so it can correct its plans and release it quicker. Nobody would be interested in Ivan's opinion about the reliability of the obsolete version. 
A bug found in the program can be of any category with equal probability. Similarly, the bug can be found in any given subsystem with equal probability. Any particular bug cannot belong to two different categories or happen simultaneously in two different subsystems. The number of bugs in the program is almost infinite, so the probability of finding a new bug of some category in some subsystem does not reduce after finding any number of bugs of that category in that subsystem. 
Find an average time (in days of Ivan's work) required to name the program disgusting.

Input

Input file contains two integer numbers, n and s (0 < n, s <= 1 000).

Output

Output the expectation of the Ivan's working days needed to call the program disgusting, accurate to 4 digits after the decimal point.

Sample Input

1 2

Sample Output

3.0000
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
double dp[1100][1000];
double n,s;int main()
{while(~scanf("%lf%lf",&n,&s)){dp[(int)n][(int)s]=0.0;for(int i=n;i>=0;i--){for(int j=s;j>=0;j--){if(i==n&&j==s)   continue;dp[i][j] = ( n*s + (n-i)*j*dp[i+1][j] + i*(s-j)*dp[i][j+1] + (n-i)*(s-j)*dp[i+1][j+1] )/( n*s - i*j );}}printf("%.4lf\n",dp[0][0]);}return 0;
}


 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/290418.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

神奇的[Caller*]属性

前言上次&#xff0c;我们《使用 CallerArgumentExpression 检查弃元参数》&#xff0c;它实际是利用编译器编译时将变量名称传入。其实&#xff0c;.NET中提供了多个[Caller*]属性&#xff0c;帮助我们轻松获取调用者信息。CallerFilePathAttribute允许获取包含调用方的源文件…

eshop截取字符串长度 和去掉省略号

<!-- {if $goods.goods_brief} --> {$goods.goods_brief|truncate:17}<!-- {/if} --> 去掉省略号&#xff1a; 找到includes/lib_base.php 第63行 $newstr . ... 去掉... 即可转载于:https://www.cnblogs.com/wesky/p/4819319.html

C和指针之字符串之实现strcpy函数

1、问题 实现strcpy函数2、代码实现 #include <stdio.h> #include <assert.h> char *str_copy(char *des, const char *src) {assert(src ! NULL);assert(des ! NULL);while ((*des *src) ! \0);return des; } int main() {const char *src "chenyu";c…

java dateTime + long

2019独角兽企业重金招聘Python工程师标准>>> public static void main(String[] args) throws Exception{SimpleDateFormat sdfnew SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // long timeStartsdf.parse("2011-09-20 12:30:45").getTime();l…

IOS 开发环境,证书和授权文件是什么?

一、成员介绍 1. Certification(证书) 证书是对电脑开发资格的认证&#xff0c;每个开发者帐号有一套&#xff0c;分为两种&#xff1a; 1) Developer Certification(开发证书) 安装在电脑上提供权限&#xff1a;开发人员通过设备进行真机测试。 可以生成副本供多台电脑安…

.NET Core中异常过滤器ExceptionFilter的使用介绍

介绍实现需要继承IExceptionFilter 或 IAsyncExceptionFilter。可用于实现常见的错误处理策略。使用场景首先讲一下我们为什么要使用异常过滤器 &#xff0c;如果业务场景复杂&#xff0c;只使用HttpStatusCode&#xff0c;抛出异常后,后期要加很多字段来描述。那么这种就比较不…

程序一启动检查网络,如果没有网络就退出程序

转载于:https://www.cnblogs.com/songxing10000/p/4823812.html

C和指针之多维数组一行存满后会轮到下一行

1、问题 比如二位数组名赋值给一个指针&#xff0c;指针在递增&#xff0c;超过这个行的最后一列后会得到怎么样结果。2、代码举例 #include <stdio.h>int main() {int a[3][3] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};int *p NULL;p &a[1][1];printf("first val…

看小说的这些年

从大一开始&#xff0c;就开始看起了小说&#xff0c;不是那种名著类型&#xff0c;而是快餐小说&#xff0c;玄幻、都市、言情、科幻&#xff0c;什么都会看&#xff0c;因为看多了&#xff0c;就会发现&#xff0c;已经没什么可以看的。 谈起快餐小说&#xff0c;已经有很多被…

如何使用 .NET Core 安全地加/解密文件

前言由于客户网络安全限制&#xff0c;连接到互联网的设备不能访问内网。需要先从客户端应用中导出数据到文件&#xff0c;再将文件复制到U盘&#xff0c;最后通过内网机器上传数据。如何保证&#xff0c;在复制、传输过程中&#xff0c;文件的安全性&#xff1f;思路首先想到的…

使用Css截取字符串

white-space:nowrap; /* 禁止自动换行 */ overflow:hidden; /* 隐藏溢出的内容 */ text-overflow:ellipsis; /* 溢出文本使用...代替 */ 转载于:https://www.cnblogs.com/xiaoxian1369/p/4083974.html

广度优先算法BFS

package myalgorithm;import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; /*BFS用于记录的位置和值的结构*/ class node {node(int xparam,int yparam,int valparam){this.x xparam;this.y yparam;this.value valparam;}int x,y,value; } publ…

COMA(一): Learning to Communicate with Deep Multi-Agent Reinforcement Learning 论文讲解

Learning to Communicate with Deep Multi-Agent Reinforcement Learning 论文讲解 论文链接&#xff1a;https://papers.nips.cc/paper/6042-learning-to-communicate-with-deep-multi-agent-reinforcement-learning.pdf &#xff08;这篇论文是COMA三部曲中的第&#xff08…

C和指针之指针数组和指向数组的指针

1、指针数组 定义一个指针数组&#xff0c;该数组中每个元素是一个指针&#xff0c;每个指针指向哪里就需要程序中后续再定义int *p[10]; 2、指向数组的指针 定义一个数组指针&#xff0c;该指针指向含10个元素的一维数组&#xff08;数组中每个元素是int型&#xff09;int (*p…

SSH 远程执行任务

SSH 是 Linux 下进行远程连接的基本工具&#xff0c;但是如果仅仅用它来登录那可是太浪费啦&#xff01;SSH 命令可是完成远程操作的神器啊&#xff0c;借助它我们可以把很多的远程操作自动化掉&#xff01;下面就对 SSH 的远程操作功能进行一个小小的总结。远程执行命令如果我…

分库分表之历史表如何选择最佳分片路由规则

前言先别急着关闭,我相信这篇文章应该是所有讲分表分库下的人都没有和你们讲过的一种分片模式,外面的文章基本上都是教你如何从零开始分片,现在我将讲解的是如何从1开始分片项目地址github地址 https://github.com/dotnetcore/sharding-coregitee地址 https://gitee.com/dotnet…

C和指针之二维字符串数组用指针数组、数组指针、二级指针打印

1、问题 二位字符串数组用指针数组、数组指针、二级指针打印 2、测试代码 #include <stdio.h>int main() {char value[4][5] = {"chen", "yu", "feng", "yy"};char *p[5];char (*p1)[5];char **p2;//把二位数组的值赋给指…

Android下强制打开软键盘

这个也是累人&#xff0c;网上查了很多方法&#xff0c;没有实现需求。 最后的实现方法很简单。代码&#xff1a; 1 //通过定时器强制打开虚拟键盘2 public static void TimerShowKeyboard(final View v)3 {4 Timer timer new Timer();5 timer.schedu…

COMA(二):Counterfactual Multi-Agent Policy Gradients 论文讲解

Counterfactual Multi-Agent Policy Gradients 论文链接&#xff1a;https://arxiv.org/pdf/1705.08926.pdf 1. 问题提出&#xff08;解决了什么问题&#xff1f;&#xff09; 在现实世界中&#xff0c;有非常多的问题需要多个单位之间的“合作”才能完成任务&#xff0c;这就…

lecture6-mini批量梯度训练及三个加速的方法

Hinton的第6课&#xff0c;这一课中最后的那个rmsprop&#xff0c;关于它的资料&#xff0c;相对较少&#xff0c;差不多除了Hinton提出&#xff0c;没论文的样子&#xff0c;各位大大可以在这上面研究研究啊。 一、mini-批量梯度下降概述 这部分将介绍使用随机梯度下降学习来训…