SPOJ:Robot(数学期望)
There is a robot on the 2D plane. Robot initially standing on the position (0, 0). Robot can make a 4 different moves:
- Up (from (x, y) to (x, y + 1)) with probability U.
- Right (from (x, y) to (x + 1, y)) with probability R.
- Down (from (x, y) to (x, y - 1)) with probability D.
- Left (from (x, y) to (x - 1, y)) with probability L.
After moving N times Robot gets points.
- Let x1 be the smallest coordinate in X-axis, that Robot reached in some moment.
- Let x2 be the largest coordinate in X-axis, that Robot reached in some moment.
- Let y1 be the smallest coordinate in Y-axis, that Robot reached in some moment.
- Let y2 be the largest coordinate in Y-axis, that Robot reached in some moment.
Points achieved by Robot equals to x2 - x1 + y2 - y1.
Given N, U, R, D, L. Calculate expected value of points that Robot achieved after N moves.
Input
First line: One interger N (1 ≤ N ≤ 200).
Second line: Four real numbers U, R, D, L (U + R + D + L = 1, 0 ≤ U, R, D, L ≤ 1) with maximum of 6 numbers after dot.
Output
One number: expected value of points achieved by Robot. The answer will be considered correct if its relative or absolute error does not exceed 10-6.
Example 1
Input:
2
0.100000 0.200000 0.300000 0.400000
Output:
1.780000
Example 2
Input:
3
0.25 0.25 0.25 0.25
Output:
2.375000
题意:二维平面上一个机器人,给出机器人走的步数,已知走上下左右的概率;机器人走到最右边是Xmax,最左边是Xmin,上下同理。问机器人Xmax-Xmin+Ymax-Ymin的期望。
思路:万万没想到,4个方向是分开求,开始一直在想怎么整体求。。。。分开求的话就不难想了,分别求出X方向的最大最小,Y方向的最大最小,搜索一下。。。自己看代码。。。但是注意搜索会超时,注意记忆化。。。
(总之,是个不错的题!
#include<bits/stdc++.h>
using namespace std;
double u,d,l,r,ans;
double dp1[][][],dp2[][][],dp3[][][],dp4[][][];
int vis1[][][],vis2[][][],vis3[][][],vis4[][][];
double maxx(int x,int R,int step)
{
if(vis1[x][R][step]) return dp1[x][R][step];
if(step==) return R;
double res=;
res+=maxx(x,R,step-)*(u+d);
res+=maxx(x+,max(R,x+),step-)*r;
res+=maxx(x-,R,step-)*l;
vis1[x][R][step]=; dp1[x][R][step]=res;
return res;
}
double minx(int x,int L,int step)
{
if(vis2[x][L][step]) return dp2[x][L][step];
if(step==) return L;
double res=;
res+=minx(x,L,step-)*(u+d);
res+=minx(x+,L,step-)*r;
res+=minx(x-,min(L,x-),step-)*l;
vis2[x][L][step]=; dp2[x][L][step]=res;
return res;
}
double maxy(int y,int U,int step)
{
if(vis3[y][U][step]) return dp3[y][U][step];
if(step==) return U;
double res=;
res+=maxy(y,U,step-)*(l+r);
res+=maxy(y+,max(U,y+),step-)*u;
res+=maxy(y-,U,step-)*d;
vis3[y][U][step]=; dp3[y][U][step]=res;
return res;
}
double miny(int y,int D,int step)
{
if(vis4[y][D][step]) return dp4[y][D][step];
if(step==) return D;
double res=;
res+=miny(y,D,step-)*(l+r);
res+=miny(y+,D,step-)*u;
res+=miny(y-,min(D,y-),step-)*d;
vis4[y][D][step]=; dp4[y][D][step]=res;
return res;
}
int main()
{
int N;
cin>>N>>u>>r>>d>>l;
ans+=maxx(,,N);
ans-=minx(,,N);
ans+=maxy(,,N);
ans-=miny(,,N);
printf("%.7lf\n",ans);
return ;
}
SPOJ:Robot(数学期望)的更多相关文章
- SPOJ FAVDICE 数学期望
题目大意: 一个有n面的色子抛掷多少次能使所有面都能被抛到过,求期望值 总面数为n,当已经抛到过 i 个不同面时,我们抛出下一个不同面的概率为 (n-i)/n,那么抛的次数为 n/(n-i) 将所有抛 ...
- 【整理】简单的数学期望和概率DP
数学期望 P=Σ每一种状态*对应的概率. 因为不可能枚举完所有的状态,有时也不可能枚举完,比如抛硬币,有可能一直是正面,etc.在没有接触数学期望时看到数学期望的题可能会觉得很阔怕(因为我高中就是这么 ...
- 动态规划之经典数学期望和概率DP
起因:在一场训练赛上.有这么一题没做出来. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6829 题目大意:有三个人,他们分别有\(X,Y,Z\)块钱 ...
- [BZOJ 3143][HNOI2013]游走(数学期望)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3143 分析: 易得如果知道了每条边经过的数学期望,那就可以贪心着按每条边的期望的大小赋 ...
- Codeforces Round #259 (Div. 2) C - Little Pony and Expected Maximum (数学期望)
题目链接 题意 : 一个m面的骰子,掷n次,问得到最大值的期望. 思路 : 数学期望,离散时的公式是E(X) = X1*p(X1) + X2*p(X2) + …… + Xn*p(Xn) p(xi)的是 ...
- 数学期望和概率DP题目泛做(为了对应AD的课件)
题1: Uva 1636 Headshot 题目大意: 给出一个000111序列,注意实际上是环状的.问是0出现的概率大,还是当前是0,下一个还是0的概率大. 问题比较简单,注意比较大小: A/C & ...
- [2013山东ACM]省赛 The number of steps (可能DP,数学期望)
The number of steps nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; ...
- 【BZOJ2134】单位错选(数学期望,动态规划)
[BZOJ2134]单位错选(数学期望,动态规划) 题面 BZOJ 题解 单独考虑相邻的两道题目的概率就好了 没了呀.. #include<iostream> #include<cs ...
- 【BZOJ1415】【NOI2005】聪聪和可可(动态规划,数学期望)
[BZOJ1415][NOI2005]聪聪和可可(动态规划,数学期望) 题面 BZOJ 题解 先预处理出当可可在某个点,聪聪在某个点时 聪聪会往哪里走 然后记忆化搜索一下就好了 #include< ...
- 【Luogu1291】百事世界杯之旅(动态规划,数学期望)
[Luogu1291]百事世界杯之旅(动态规划,数学期望) 题面 洛谷 题解 设\(f[i]\)表示已经集齐了\(i\)个名字的期望 现在有两种方法: 先说我自己的: \[f[i]=f[i-1]+1+ ...
随机推荐
- AC日记——[USACO1.5]数字三角形 Number Triangles 洛谷 P1216
题目描述 观察下面的数字金字塔. 写一个程序来查找从最高点到底部任意处结束的路径,使路径经过数字的和最大.每一步可以走到左下方的点也可以到达右下方的点. 7 3 8 8 1 0 2 7 4 4 4 5 ...
- windows安装RabbitMQ注意事项
1.首先下载好ERLANG.RabbitMQ安装包,先安装erlang,设置好环境变量,然后再去安装MQ; 2.别人有两个报错: 一:RabbitMQ安装目录中不允许有空格; 二:安装rabbitmq ...
- 解决maven无法下载依赖的jar包的问题
背景: 公司内部有搭建maven私服,自己做了个核心jar包,一开始是xxx-core.1.0.0.SNAPSHOT版本,是本地和项目环境都可以正常使用的.为支持上线,发布稳定版本,xxx-core. ...
- 洛谷 P2831 愤怒的小鸟
P2831 愤怒的小鸟 题目描述 Kiana 最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于 (0,0)(0,0) 处,每次 Kiana 可以用它向第一象 ...
- MVP模式是你的救命稻草吗?
为什么要学习架构? 不管是MVC还是MVP,亦或则其他架构,它们的设计目的都是为了达到编码的最高境界,那就是:低藕合,高复用,易测试,好维护. 而要达到这个终极目标,首先要理解的是每个部分各自负责些什 ...
- js 中 Map/Set 集合
Map Map是一组键值对的结构,具有极快的查找速度. 举个例子,假设要根据同学的名字查找对应的成绩,如果用Array实现,需要两个Array: 1 var names = ['Michael', ...
- PHP中extract()函数的妙用
看cakephp 2.3.8的源代码,很多地方都用 到 compact('name', 'response'); extract($status, EXTR_OVERWRITE); 这样的代码.com ...
- chrome插件网站
chrome插件网站 http://chromecj.com/
- 转:Twemproxy——针对MemCached与Redis的代理
转自: http://www.infoq.com/cn/news/2012/12/twemproxy Twemproxy是一个代理服务器,可以通过它减少Memcached或Redis服务器所打开的连接 ...
- 用Perl发送邮件小例子
据传,Perl发送邮件有很多方案,但我只会用Mail::Sender这种方式,也就只能简单谈谈这种方式. 在参考众多网页后,程序书写如下: #!/usr/bin/perl -w use Mail::S ...