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[410][410][210],dp2[410][410][210],dp3[410][410][210],dp4[410][410][210]; int vis1[410][410][210],vis2[410][410][210],vis3[410][410][210],vis4[410][410][210]; double maxx(int x,int R,int step) { if(vis1[x][R][step]) return dp1[x][R][step]; if(step==0) return R; double res=0; res+=maxx(x,R,step-1)*(u+d); res+=maxx(x+1,max(R,x+1),step-1)*r; res+=maxx(x-1,R,step-1)*l; vis1[x][R][step]=1; 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==0) return L; double res=0; res+=minx(x,L,step-1)*(u+d); res+=minx(x+1,L,step-1)*r; res+=minx(x-1,min(L,x-1),step-1)*l; vis2[x][L][step]=1; 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==0) return U; double res=0; res+=maxy(y,U,step-1)*(l+r); res+=maxy(y+1,max(U,y+1),step-1)*u; res+=maxy(y-1,U,step-1)*d; vis3[y][U][step]=1; 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==0) return D; double res=0; res+=miny(y,D,step-1)*(l+r); res+=miny(y+1,D,step-1)*u; res+=miny(y-1,min(D,y-1),step-1)*d; vis4[y][D][step]=1; dp4[y][D][step]=res; return res; } int main() { int N; cin>>N>>u>>r>>d>>l; ans+=maxx(201,201,N); ans-=minx(201,201,N); ans+=maxy(201,201,N); ans-=miny(201,201,N); printf("%.7lf ",ans); return 0; }