Broken robot CodeForces - 24D (概率DP)
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
10 10
10 4
0.0000000000
10 14
5 14
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)的更多相关文章
- Broken robot CodeForces - 24D (三对角矩阵简化高斯消元+概率dp)
题意: 有一个N行M列的矩阵,机器人最初位于第i行和第j列.然后,机器人可以在每一步都转到另一个单元.目的是转到最底部(第N个)行.机器人可以停留在当前单元格处,向左移动,向右移动或移动到当前位置下方 ...
- Codeforces 28C [概率DP]
/* 大连热身D题 题意: 有n个人,m个浴室每个浴室有ai个喷头,每个人等概率得选择一个浴室. 每个浴室的人都在喷头前边排队,而且每个浴室内保证大家都尽可能均匀得在喷头后边排队. 求所有浴室中最长队 ...
- codeforces 148D 概率DP
题意: 原来袋子里有w仅仅白鼠和b仅仅黑鼠 龙和王妃轮流从袋子里抓老鼠. 谁先抓到白色老师谁就赢. 王妃每次抓一仅仅老鼠,龙每次抓完一仅仅老鼠之后会有一仅仅老鼠跑出来. 每次抓老鼠和跑出来的老鼠都是随 ...
- codeforces 540D 概率dp
传送门 大概可以这样理解, 一开始有r个石头, p个布, s个剪刀, 每一天有其中的两个相遇, 如果两个是相同的种类, 什么都不会发生, 否则的话有一个会挂掉, 问最后每一种生存的概率. dp[i][ ...
- CodeForces 398B 概率DP 记忆化搜索
题目:http://codeforces.com/contest/398/problem/B 有点似曾相识的感觉,记忆中上次那个跟这个相似的 我是用了 暴力搜索过掉的,今天这个肯定不行了,dp方程想了 ...
- Codeforces 931 概率DP
A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...
- Codeforces - 518D 概率DP初步
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #i ...
- Vasya and Magic Matrix CodeForces - 1042E (概率dp)
大意:给定n*m矩阵, 初始位置(r,c), 每一步随机移动到权值小于当前点的位置, 得分为移动距离的平方, 求得分期望. 直接暴力dp的话复杂度是O(n^4), 把距离平方拆开化简一下, 可以O(n ...
- CodeForces 24D Broken robot (概率DP)
D. Broken robot time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
随机推荐
- mysql 链接时报错:1251-Client does not support authentication protocol requested by server
一 原因是mysql服务器要求的认证插件版本与客户端不一致造成的. 二 由于我是最新的mysql和破解版的navicat,那么就是mysql太高级了. 解决方法有两个,我毫不犹豫的选择mysql降级. ...
- PIO导出
1..HSSFWorkbook 声明一个工作簿,创建一个excel文件 //创建HSSFWork对象(excel的文档对象) HSSFWorkbook wb=new HSSFWorkbook(); / ...
- 洛谷 P1281 书的复制
书的复制 Code: #include <iostream> #include <cstdio> #include <cstring> using namespac ...
- <Android 应用 之路> 天气预报(五)
前言 写了上一篇文章,讲了下这个实现天气预报的使用内容,现在又到了看代码的时候,主要还是贴代码,然后添加足够的注释. 聚合数据SDK配置 将juhe_sdk_v_X_X.jar以及armeabi文件夹 ...
- Android 使用GreenDao 添加字段,删除表,新增表操作
GreenDao 给我个人感觉 比一般的ORM框架要好很多,虽然说上手和其他的比起来,较复杂,但是如果使用熟练以后,你会爱上这个框架的 用这些ORM 框架给我的感觉都是,当升级时,都需要进行数据库所有 ...
- 【Linux/Ubuntu学习 11】git查看某个文件的修改历史
有时候在比对代码时,看到某些改动,但不清楚这个改动的作者和原因,也不知道对应的BUG号,也就是说无从查到这些改动的具体原因了- [注]:某个文件的改动是有限次的,而且每次代码修改的提交都会有commi ...
- 六、C++离散傅里叶逆变换
C++离散傅里叶逆变换 一.序言: 该教程承接上文的离散傅里叶变换,用于进行离散傅里叶逆变换. 二.设计目标 对复数数组进行离散傅里叶逆变换,并生成可供使用的图像类. 三.详细步骤 输入:经傅里叶变换 ...
- JavaWeb中五种转发方式(转)
今天本来是想找一下在jsp中实现转发的方式的,无意中看到了一篇文章,然后稍微综合了把服务器端的转发也包括在内. 1. RequestDispatcher.forward() 是在服务器端起作用,当 ...
- js实现排序去重计算字符次数
/*去重*/ var arr=[1,4,4,7,3,9,0,3,2,1,"你好","你","你好","你 "]; var ...
- android intent filter浏览器应用的设置,如何使用choose-box选择应用
//使用chooserIntent private void startImplicitActivation() { Log.i(TAG, "Entered startImplicitAct ...