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 ...
随机推荐
- sass 的使用心得
//定义颜色 $c55:#; $c22:#; $c33:#; $c99:#; $c77:#; $c00:#; $cff:#fff; $caa:#aaa; $ccc:#ccc; $cf0:#f0f0f0 ...
- ubuntu 16.04LTS
安装出现"server64 busybox-initramfs安装失败" 这个是BUG,解决方法一:初次安装选择语言时,使用English,在后面还会有一个选择语言的界面,这时候再 ...
- JSON特殊字符的处理
本文转自:http://blog.csdn.net/btt2013/article/details/66970735 JSon 数据中的String 传递数据时,需要处理好特殊字符. JSon数据中有 ...
- select下拉选框的默认值,包括每次进入页面的默认值
下拉选: <select onchange="selectTotal(this.value)" style="width: 50px;"> ...
- DIV中display和visibility属性差别
DIV中display和visibility属性差别 DIV中display和visibility属性差别还是挺大的,虽然Visibility和Display属性都可以达到隐藏页面元素的目的,但它们的 ...
- Laravel 5.1 中创建自定义 Artisan 控制台命令实例教程
1.入门 Laravel通过Artisan提供了强大的控制台命令来处理非浏览器业务逻辑.要查看Laravel中所有的Artisan命令,可以通过在项目根目录运行: php artisan list 对 ...
- jQuery补充,基于jQuery的bxslider轮播器插件
基于jQuery的bxslider轮播器插件 html <!DOCTYPE html> <html lang="zh-cn"> <head> & ...
- ZooKeeper是以Fast Paxos算法为基础的
ZooKeeper是以Fast Paxos算法为基础的,Paxos 算法存在活锁的问题,即当有多个proposer交错提交时,有可能互相排斥导致没有一个proposer能提交成功,而Fast Paxo ...
- PHP常用技术文之文件操作和目录操作总结
<?php header("content-type:text/html;charset=utf-8"); /* *声明一个函数,传入文件名获取文件属性 *@param st ...
- Html制作简单而漂亮的登录页面
先来看看样子. html源码: <!DOCTYPE html> <html lang="en"> <head> <meta charset ...