CodeForces 24D Broken robot (概率DP)
2 seconds
256 megabytes
standard input
standard output
You received as a gift a very clever robot walking on a rectangular board. Unfortunately, you understood that it is broken and behaves rather strangely (randomly). The board consists of N rows
and Mcolumns of cells. The robot is initially at some cell on the i-th
row and the j-th column. Then at every step the robot could go to some another cell. The aim is to go to the bottommost (N-th)
row. The robot can stay at it's current cell, move to the left, move to the right, or move to the cell below the current. If the robot is in the leftmost column it cannot move to the left, and if it is in the rightmost column it cannot move to the right. At
every step all possible moves are equally probable. Return the expected number of step to reach the bottommost row.
On the first line you will be given two space separated integers N andM (1 ≤ N, M ≤ 1000).
On the second line you will be given another two space separated integers i and j (1 ≤ i ≤ N, 1 ≤ j ≤ M)
— the number of the initial row and the number of the initial column. Note that, (1, 1) is the upper left corner of the board and (N, M) is
the bottom right corner.
Output the expected number of steps on a line of itself with at least 4digits after the decimal point.
10 10
10 4
0.0000000000
10 14
5 14
18.0038068653
概率DP
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <vector> using namespace std;
int n,m;
int x,y;
double dp[1005][1005];
int main()
{
scanf("%d%d",&n,&m);
scanf("%d%d",&x,&y);
double p1=1.0/3;
double p2=1.0/4;
double p3=1.0/2;
for(int i=n-1;i>=x;i--)
{
for(int t=0;t<=50;t++)
{
for(int j=1;j<=m;j++)
{
if(m==1)
dp[i][j]=(dp[i+1][j]*p3+1)/(1-p3);
else if(j==1)
dp[i][j]=(dp[i][j+1]*p1+dp[i+1][j]*p1+1)/(1-p1);
else if(j==m)
dp[i][j]=(dp[i][j-1]*p1+dp[i+1][j]*p1+1)/(1-p1);
else
dp[i][j]=(dp[i][j+1]*p2+dp[i][j-1]*p2+dp[i+1][j]*p2+1)/(1-p2);
}
}
}
printf("%.6f\n",dp[x][y]);
return 0;
}
CodeForces 24D Broken robot (概率DP)的更多相关文章
- Codeforces.24D.Broken robot(期望DP 高斯消元)
题目链接 可能这儿的会更易懂一些(表示不想再多写了). 令\(f[i][j]\)表示从\((i,j)\)到达最后一行的期望步数.那么有\(f[n][j]=0\). 若\(m=1\),答案是\(2(n- ...
- CodeForces 24D Broken robot(期望+高斯消元)
CodeForces 24D Broken robot 大致题意:你有一个n行m列的矩形板,有一个机器人在开始在第i行第j列,它每一步会随机从可以选择的方案里任选一个(向下走一格,向左走一格,向右走一 ...
- CodeForces 24D Broken Robot
题意:n*m的棋盘,一个机器人在(i,j)处,每次等概率地停在原地,向左移动一格,向右移动一格,向下移动一格(不能移出棋盘).求走到最后一行所需期望步数.n<=1000,m<=1000 一 ...
- codeforces 24d Broken robot 期望+高斯消元
题目传送门 题意:在n*m的网格上,有一个机器人从(x,y)出发,每次等概率的向右.向左.向下走一步或者留在原地,在最左边时不能向右走,最右边时不能像左走.问走到最后一行的期望. 思路:显然倒着算期望 ...
- HDU 4576 Robot(概率dp)
题目 /*********************复制来的大致题意********************** 有N个数字,M个操作, 区间L, R. 然后问经过M个操作后落在[L, R]的概率. * ...
- CodeForces - 24D :Broken robot (DP+三对角矩阵高斯消元 随机)
pro:给定N*M的矩阵,以及初始玩家位置. 规定玩家每次会等概率的向左走,向右走,向下走,原地不动,问走到最后一行的期望.保留4位小数. sol:可以列出方程,高斯消元即可,发现是三角矩阵,O(N* ...
- BZOJ 3270 博物馆 && CodeForces 113D. Museum 期望概率dp 高斯消元
大前提,把两个点的组合看成一种状态 x 两种思路 O(n^7) f[x]表示在某一个点的前提下,这个状态经过那个点的概率,用相邻的点转移状态,高斯一波就好了 O(n^6) 想象成臭气弹,这个和那个的区 ...
- 【CF24D】Broken Robot (DP+高斯消元)
题目链接 题意:给定一个\(n\times m\)的矩阵,每次可以向→↓←移动一格,也可以原地不动,求从\((x,y)\)到最后一行的期望步数. 此题标签\(DP\) 看到上面这个肯定会想到 方法一: ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
随机推荐
- LeetCode总结 -- 树的求和篇
树的求和属于树的题目中比較常见的,由于能够有几种变体,灵活度比較高,也能够考察到对于树的数据结构和递归的理解. 一般来说这些题目就不用考虑非递归的解法了(尽管事实上道理是跟LeetCode总结 -- ...
- spring 学习资料备份
易百教程 https://www.yiibai.com/spring/spring-autowiring-by-name.html
- android.graphics(1) - Paint, Canvas, drawLine, drawPoint, drawRect, drawRoundRect, drawCircle, drawOval, drawArc
一.Paint与Canvas 像我们平时画图一样,需要两个工具,纸和笔.Paint就是相当于笔,而Canvas就是纸,这里叫画布. 所以,凡有跟要要画的东西的设置相关的,比如大小,粗细,画笔颜色,透明 ...
- yii2 beta版 执行流程
yii2 beta版 执行流程 自动加载 1.composer的自动加载 //composer的加载实现了四种方式,可以看看 require(__DIR__ . '/../../vendor/auto ...
- vue知识点2018.6.3
文件夹和文件名称 简介 build 构建脚本目录 config 应用程序的配置文件 index.html 入口页面 node_modules 存放 NPM 依赖模块 package-lock.json ...
- dfs带状态改变的做法
所谓带状态改变是指:在搜索到某个位置的时候,状态发生改变,继续计算步数. 给一个例题: 蒜头君要回家,但是他家的钥匙在他的朋友花椰妹手里,他要先从花椰妹手里取得钥匙才能回到家.花椰妹告诉他:“你家的钥 ...
- 扩展RBAC用户角色权限设计方案<转>
RBAC(Role-Based Access Control,基于角色的访问控制),就是用户通过角色与权限进行关联.简单地说,一个用户拥有若干角色,每一个角色拥有若干权限.这样,就构造成“用户-角色- ...
- Supervisor重新加载配置启动新的进程
一.添加好配置文件后 二.更新新的配置到supervisord supervisorctl update 三.重新启动配置中的所有程序 supervisorctl reload 四.启动某个进程(pr ...
- 大数据(11) - kafka的安装与使用
一.Kafka概述 1.Kafka是什么 在流式计算中,Kafka一般用来缓存数据,Storm通过消费Kafka的数据进行计算. 1)Apache Kafka是一个开源消息系统,由Scala写成.是由 ...
- easy UI动态赋值
1,首先怎么清除这个值 $('#filegrid').datagrid('loadData', { total: 0, rows: [] }); 2,清除后,通过post提交请求,怎么将新植穿进去,这 ...