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 M columns 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.

Input

On the first line you will be given two space separated integers N and M (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

Output the expected number of steps on a line of itself with at least 4 digits after the decimal point.

Examples

Input
10 10
10 4
Output
0.0000000000
Input
10 14
5 14
Output
18.0038068653

题意:一个N*M的矩阵,一个机器人站在x,y的位置,每次可以走四种:1.呆在原地 2.向下一步 3.向左一步 4.向右一步。问走到最后一行走的步数的期望是多少
题解:网上有许多高斯消元的方法,这道题也可以多次逼近写,直接推出转移方程。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<cstdlib>
#include <vector>
#include<queue>
using namespace std; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
const int maxn = 1e5+5;
const int mod = 1e9+7; double f[1005][1005]; int main()
{
int N,M;
scanf("%d %d",&N,&M);
int x,y;
scanf("%d %d",&x,&y);
for(int i=N-1;i>=x;i--)
{
for(int k=1;k<=50;k++)
{
for(int j=1;j<=M;j++)
{
if(M == 1)
f[i][1] = (f[i][1] + f[i+1][1])/2 + 1;
else if(j == 1)
f[i][1] = (f[i][1] + f[i][2] + f[i+1][1])/3 + 1;
else if(j == M)
f[i][M] = (f[i][M] + f[i][M-1] + f[i+1][M])/3 + 1;
else
f[i][j] = (f[i][j-1] + f[i][j] + f[i][j+1] + f[i+1][j])/4 + 1;
}
}
}
printf("%lf\n",f[x][y]);
}

Broken robot CodeForces - 24D (概率DP)的更多相关文章

  1. Broken robot CodeForces - 24D (三对角矩阵简化高斯消元+概率dp)

    题意: 有一个N行M列的矩阵,机器人最初位于第i行和第j列.然后,机器人可以在每一步都转到另一个单元.目的是转到最底部(第N个)行.机器人可以停留在当前单元格处,向左移动,向右移动或移动到当前位置下方 ...

  2. Codeforces 28C [概率DP]

    /* 大连热身D题 题意: 有n个人,m个浴室每个浴室有ai个喷头,每个人等概率得选择一个浴室. 每个浴室的人都在喷头前边排队,而且每个浴室内保证大家都尽可能均匀得在喷头后边排队. 求所有浴室中最长队 ...

  3. codeforces 148D 概率DP

    题意: 原来袋子里有w仅仅白鼠和b仅仅黑鼠 龙和王妃轮流从袋子里抓老鼠. 谁先抓到白色老师谁就赢. 王妃每次抓一仅仅老鼠,龙每次抓完一仅仅老鼠之后会有一仅仅老鼠跑出来. 每次抓老鼠和跑出来的老鼠都是随 ...

  4. codeforces 540D 概率dp

    传送门 大概可以这样理解, 一开始有r个石头, p个布, s个剪刀, 每一天有其中的两个相遇, 如果两个是相同的种类, 什么都不会发生, 否则的话有一个会挂掉, 问最后每一种生存的概率. dp[i][ ...

  5. CodeForces 398B 概率DP 记忆化搜索

    题目:http://codeforces.com/contest/398/problem/B 有点似曾相识的感觉,记忆中上次那个跟这个相似的 我是用了 暴力搜索过掉的,今天这个肯定不行了,dp方程想了 ...

  6. Codeforces 931 概率DP

    A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...

  7. Codeforces - 518D 概率DP初步

    #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #i ...

  8. Vasya and Magic Matrix CodeForces - 1042E (概率dp)

    大意:给定n*m矩阵, 初始位置(r,c), 每一步随机移动到权值小于当前点的位置, 得分为移动距离的平方, 求得分期望. 直接暴力dp的话复杂度是O(n^4), 把距离平方拆开化简一下, 可以O(n ...

  9. CodeForces 24D Broken robot (概率DP)

    D. Broken robot time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

随机推荐

  1. Mybatis-Plus使用全解

    前言 之前写了<SpringBoot | 第九章:Mybatis-plus的集成和使用>一文,只是简单的使用条件构造器列举了一些通用的CURD操作.本人也想写一篇通用的关于mybatis- ...

  2. SpringMVC03 ParameterMethodNameResolver(参数方法名称解析器) And XmlViewResolver(视图解析器)

    参数方法名称解析器 1.配置依赖包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht ...

  3. Erlang 001--开篇

    有段时间没有更新博客了,最近稍微接触了下一门相对小众的语言Erlang,个人感觉学习一段时间有必要总结总结,本文作为该系列的开篇,仅仅列举一些与Java的一些不同点和个人对Erlang的一些主观印象, ...

  4. JDBC连接数据库(Servlet+JSP)

    JDBC(Java Database connectivity),是连接数据库的一种方式.后面的框架Mybatis和Hibernate等都封装的是JDBC.在JDBC中常用的API有4个:Driver ...

  5. cron 任务执行表达式

    1.来源 开始我还不知道cron到底来源于哪里,不求甚解的我也没做过多了解,现在突然用到所以写一下. cron计划任务  其实只是linux 一个执行计划的一个工具或者执行程序. 在Linux系统中, ...

  6. web安全防御之RASP技术

    作者:      我是小三 博客:      http://www.cnblogs.com/2014asm/ 由于时间和水平有限,本文会存在诸多不足,希望得到您的及时反馈与指正,多谢! 0x00:we ...

  7. 《Python高效开发实战》实战演练——建立应用2

    为了在项目中开发符合MVC架构的实际应用程序,需要在项目中建立Django应用.每个Django项目可以包含多个Django应用.建立应用的语法为: #python manage.pystartapp ...

  8. cms-详细页面2

    详细页面遗留下来的部分: 1:当前位置 2.分享 3.时间格式 4.摘要 5.关键字: 解决方案: 1:当前位置:---后台拼接 2:分享:前端一段js代码 3.摘要,直接数据库查询 4.时间格式:引 ...

  9. C# 字符串转组件名、变量名

    字符串转组件名 (Controls["button1"] as Button).Text = "Hello";//单独组件 (Controls[].Contro ...

  10. PHP中MySQL数据库连接,数据读写,修改方法

    MySQL连接大的来说有两种方法,一种是mysqli,另一种是mysql.php为连接MySQL提供了函数库,有mysql和mysqli,mysqli是mysql函数库的扩展,是php5才支持的.当你 ...