HDU 2262 Where is the canteen 期望dp+高斯消元
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=2262
Where is the canteen
Time Limit: 10000/5000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)
#### 问题描述
> After a long drastic struggle with himself, LL decide to go for some snack at last. But when steping out of the dormitory, he found a serious problem : he can't remember where is the canteen... Even worse is the campus is very dark at night. So, each time he move, he check front, back, left and right to see which of those four adjacent squares are free, and randomly walk to one of the free squares until landing on a canteen.
#### 输入
> Each case begin with two integers n and m ( n
> '@' is the start location. There is exactly one in each case.
> '#' is an impassible square.
> '$' is a canteen. There may be more than one in the campus.
> '.' is a free square.
#### 输出
> Output the expected number of moves required to reach a canteen, which accurate to 6 fractional digits. If it is impossible , output -1.
####样例输入
> 1 2
> @$
> 2 2
> @.
> .$
> 1 3
> @#$
样例输出
1.000000
4.000000
-1
题意
给你校园地图:n*m的网格,现在有个人从宿舍出来,要去食堂,由于天太黑,他看不见路,所以他会随机走到相邻的可以到达的点(也就是不是边界和墙,走过的点也还有可能走),问他到达食堂的期望步数。
题解
dp[i][j]表示在i,j点到达食堂需要的期望步数。
根据全期望公式易得:dp[i*m+j]=(ne1+ne2+...+nek)/k+1
明显有环,不能直接递推。数据很小,我们处理下式子:ne1+ne2+...+nek-k*dp[i*m+j]=-k
。然后直接上高斯消元。
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-9;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=233;
typedef double Matrix[maxn][maxn];
///n*(n+1)的增广矩阵
bool gauss_jordan(Matrix A,int n) {
int i,j,k,r;
for(i=0; i<n; i++) {
r=i;
for(j=i+1; j<n; j++) {
if(fabs(A[j][i])>fabs(A[r][i])) r=j;
}
if(fabs(A[r][i])<eps) continue;
if(r!=i) for(j=0; j<=n; j++) swap(A[r][j],A[i][j]);
for(k=0; k<n; k++) if(k!=i) {
for(j=n; j>=i; j--) A[k][j]-=A[k][i]/A[i][i]*A[i][j];
}
}
///矛盾式
for(int i=n-1; i>=0&&fabs(A[i][i])<eps; i--) {
if(fabs(A[i][n])>eps) return false;
}
return true;
}
Matrix A;
char mat[22][22];
int n,m;
bool vis[22][22];
const int dx[]= {-1,1,0,0};
const int dy[]= {0,0,-1,1};
///求起点所在的联通块,其他不连通的地区不要啦进来算
bool flag;
void dfs(int x,int y) {
vis[x][y]=1;
if(mat[x][y]=='$') flag=true;
for(int i=0; i<4; i++) {
int nx=x+dx[i];
int ny=y+dy[i];
if(nx<0||nx>=n||ny<0||ny>=m) continue;
if(vis[nx][ny]) continue;
if(mat[nx][ny]=='#') continue;
dfs(nx,ny);
}
}
void init() {
clr(vis,0);
clr(A,0);
}
int main() {
while(scf("%d%d",&n,&m)==2) {
init();
for(int i=0; i<n; i++) scf("%s",mat[i]);
int xs,ys;
rep(i,0,n) rep(j,0,m) if(mat[i][j]=='@') {
xs=i,ys=j;
break;
}
flag=false;
dfs(xs,ys);
///根本到不了任何食堂
if(!flag){
puts("-1");
continue;
}
///构造方程组
int nn=n*m;
rep(i,0,n) rep(j,0,m) {
if(!vis[i][j]) continue;
if(mat[i][j]=='$') {
A[i*m+j][i*m+j]=1.0;
} else {
int k=0,ix=i*m+j;
for(int ii=0; ii<4; ii++) {
int nx=i+dx[ii];
int ny=j+dy[ii];
if(nx<0||nx>=n||ny<0||ny>=m) continue;
if(!vis[nx][ny]) continue;
k++;
int iy=nx*m+ny;
A[ix][iy]=1.0;
}
A[ix][ix]=-1.0*k;
A[ix][nn]=-1.0*k;
}
}
bool su=gauss_jordan(A,nn);
int p=xs*m+ys;
if(!su||fabs(A[p][p])<eps) prf("-1\n");
else prf("%.6lf\n",A[p][nn]/A[p][p]);
}
return 0;
}
//end-----------------------------------------------------------------------
HDU 2262 Where is the canteen 期望dp+高斯消元的更多相关文章
- BZOJ_3143_[Hnoi2013]游走_期望DP+高斯消元
BZOJ_3143_[Hnoi2013]游走_期望DP+高斯消元 题意: 一个无向连通图,顶点从1编号到N,边从1编号到M. 小Z在该图上进行随机游走,初始时小Z在1号顶点,每一步小Z以相等的概率随机 ...
- hdu4418 Time travel 【期望dp + 高斯消元】
题目链接 BZOJ4418 题解 题意:从一个序列上某一点开始沿一个方向走,走到头返回,每次走的步长各有概率,问走到一点的期望步数,或者无解 我们先将序列倍长形成循环序列,\(n = (N - 1) ...
- 【noi2019集训题1】 脑部进食 期望dp+高斯消元
题目大意:有n个点,m条有向边,每条边上有一个小写字母. 有一个人从1号点开始在这个图上随机游走,游走过程中他会按顺序记录下走过的边上的字符. 如果在某个时刻,他记录下的字符串中,存在一个子序列和S2 ...
- LightOJ 1151 Snakes and Ladders 期望dp+高斯消元
题目传送门 题目大意:10*10的地图,不过可以直接看成1*100的,从1出发,要到达100,每次走的步数用一个大小为6的骰子决定.地图上有很多个通道 A可以直接到B,不过A和B大小不确定 而且 ...
- P4457-[BJOI2018]治疗之雨【期望dp,高斯消元】
正题 题目链接:https://www.luogu.com.cn/problem/P4457 题目大意 开始一个人最大生命值为\(n\),剩余\(hp\)点生命,然后每个时刻如果生命值没有满那么有\( ...
- ZJUT 1423 地下迷宫(期望DP&高斯消元)
地下迷宫 Time Limit:1000MS Memory Limit:32768K Description: 由于山体滑坡,DK被困在了地下蜘蛛王国迷宫.为了抢在DH之前来到TFT,DK必须尽快走 ...
- Codeforces.24D.Broken robot(期望DP 高斯消元)
题目链接 可能这儿的会更易懂一些(表示不想再多写了). 令\(f[i][j]\)表示从\((i,j)\)到达最后一行的期望步数.那么有\(f[n][j]=0\). 若\(m=1\),答案是\(2(n- ...
- HDU4418 Time travel(期望dp 高斯消元)
题意 题目链接 Sol mdzz这题真的太恶心了.. 首先不难看出这就是个高斯消元解方程的板子题 \(f[x] = \sum_{i = 1}^n f[to(x + i)] * p[i] + ave\) ...
- [BZOJ3150][Ctsc2013]猴子 期望dp+高斯消元
3150: [Ctsc2013]猴子 Time Limit: 20 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 163 Solved: 10 ...
随机推荐
- MySql优化分析
原理 MYSQL逻辑分层 :连接层 服务层 引擎层 存储层 InnoDB(默认) :事务优先 (适合高并发操作:行锁) MyISAM :性能优先 (表锁) SQL优化 编写过程: sql select ...
- PHP:CURL分别以GET、POST方式请求HTTPS协议接口api
1.curl以GET方式请求https协议接口 //注意:这里的$url已经包含参数了,不带参数你自己处理哦GET很简单 function curl_get_https($url){ $curl = ...
- 关于IRAM和IFLASH启动模式,重映射remap 整理中
工程基于NXP LPC2468 1 为什么试用IRAM MODE 2 设置Program algorithm 编程算法的作用是什么 3 IRAM和FLASH 模式下IROM和IRAM的地址为什么不一样 ...
- 利用IPC通道进行进程间通信(C#)
有一个解决方案,其中包括一个Windows服务和一个Windows应用程序,两者之间需要进行通信.查了下,可以使用多种方法,如Web service(适用于不同系统及跨平台情况)..NET Remot ...
- 大数据入门第八天——MapReduce详解(三)MR的shuffer、combiner与Yarn集群分析
/mr的combiner /mr的排序 /mr的shuffle /mr与yarn /mr运行模式 /mr实现join /mr全局图 /mr的压缩 今日提纲 一.流量汇总排序的实现 1.需求 对日志数据 ...
- Java基础——类加载机制
什么叫类加载 JVM把 .class 字节码文件加载到内存,并进行相关的校验.解析.初始化,最终转换为虚拟机可用的JAVA类型的过程,称为JVM类加载机制. (当然,JVM并不关心class文件的来源 ...
- 20155236范晨歌_MSF基础应用
20155236范晨歌_MSF基础应用 20155236范晨歌_MSF基础应用 目录 概述 MS08-067漏洞攻击 MS11-050漏洞攻击 MS10-087漏洞攻击 辅助模块 概述 MSF的六种模 ...
- jquery ajax异步提交表单数据
使用jquery的ajax方法可以异步提交表单,成功后后台返回json数据,回调函数处理,可以不用刷新页面,达到异步的目的: 处理表单的数据可以用serialize()方法进行序列化,而如果提交的数据 ...
- [ATL/WTL]_[初级]_[选择目录对话框]
场景 1.起因是创建标准选择目录对话框时使用了 SHCreateItemFromParsingName 函数, 这个函数支持vista以上系统. 之后再winxp上运行就报错: 无法定位程序输入点 S ...
- 柯朗微积分与数学分析习题选解(1.3 节 b)
一直在读<陶哲轩实分析>,陶的书非常的严谨,环环相扣,但是也有个缺点就是计算性的例子和应用方面的例子太少了.所以就又找了本柯朗的<微积分与数学分析>搭配着看.柯朗的书的习题与陶 ...