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+ ...
随机推荐
- SGU 107 数学题
题意:求平方后末尾9个数是987654321的数个数. 之前做此题,竟然愚蠢到用计算器 在哪里算,还加笔算,SB啊!不知道先打印一下吗! #include<iostream> #inclu ...
- T13432 1.计数
题目描述 给出m个数a[1],a[2],…,a[m] 求1~n中有多少数不是a[1],a[2],…,a[m]的倍数. 输入输出格式 输入格式: 输入文件名为count.in. 第一行,包含两个整数:n ...
- mysql中进行删除操作时用到not in 导致删除不成功
delete from tb_news where id not in ( select max(id) From tb_news Group By title ) 刚开始用这条语句删除一直不成功 然 ...
- Hibernate 与Mybatis之比较
1. hibernate是全自动,而mybatis是半自动.hibernate完全可以通过对象关系模型实现对数据库的操作,拥有完整的JavaBean对象与数据库的映射结构来自动生成sql.而mybat ...
- websocket笔记
本文为原创,转载请注明出处: cnzt 文章:cnzt-p http://www.cnblogs.com/zt-blog/p/6742746.html websocket -- 双向通信网 ...
- Android中的多线程编程(一)附源代码
Android中多线程编程:Handler类.Runnable类.Thread类之概念分析 1.Handler类: Handler是谷歌封装的一种机制:能够用来更新UI以及消息的发送和处理.Handl ...
- 【Todo】RTP/RTCP/RTSP/SIP/SDP 等多媒体传输和会话协议
参考 http://m.blog.csdn.net/article/details?id=6211447
- BUPT复试专题—哈夫曼树(2010)
https://www.nowcoder.com/practice/162753046d5f47c7aac01a5b2fcda155?tpId=67&tqId=29635&tPage= ...
- expect实现无交互操作
按两下tab linux总共2000个命令,,常用的200个命令. 只要文件改变了,MD5值就会变!
- 图片异步载入之 Android-Universal-Image-Loader
今天在做项目的时候用了之前写的图片载入类.尽管也能实现缓存什么的.可是在载入大图的时候非常慢非常慢.于是上网找解决方式,准备优化一下,无意中发现了Android-Universal-Image-Loa ...