hdu2262 Where is the canteen
Where is the canteen
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1313 Accepted Submission(s): 384
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.
'@' 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.
@$
2 2
@.
.$
1 3
@#$
4.000000
-1
/*
将点所有点令为1至n*m-1那么建立n*m个方程
对于一个任一个点En=(E1+E2+E3)/cnt+1; cnt为可以走的方向数 [0,4]
最后用高斯消元模版求解(用kuangbin的模版不知为什么这么慢)
而且餐厅有很多个
*/
#include<iostream>
#include<cmath>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std; #define eps 1e-12
const int MAXN = 250;
char mp[20][20];
bool vis[20][20];
int n,m;
int sx,sy;
int dx[4]={-1,1,0,0};
int dy[4]={0,0,1,-1};
struct Node{
int x,y;
}aa,bb;
queue<Node>q;
int temp[MAXN];
double a[MAXN][MAXN];
double x[MAXN];
int equ,var; inline int C(int x,int y){
return x*m+y;
}
bool Ok(int x,int y,int d){
if(d==0){
if(x>=0 && x<n && y>=0 && y<m && mp[x][y]!='#' && !vis[x][y]) return true;
}else {
if(x>=0 && x<n && y>=0 && y<m && mp[x][y]!='#' && vis[x][y]) return true;
}
return false;
}
void Bfs(){
int i,j,k;
while(!q.empty()){
bb=q.front(); q.pop();
for(i=0;i<4;i++){
aa.x=bb.x+dx[i];
aa.y=bb.y+dy[i];
if(Ok(aa.x,aa.y,0)){
vis[aa.x][aa.y]=1;
q.push(aa);
}
}
}
} void Makefunction(){
int i,j,k;
for(i=0;i<n;i++)
for(j=0;j<m;j++){
int cnt=0;
if(mp[i][j]=='#') continue;
if(mp[i][j]=='$'){
a[C(i,j)][C(i,j)]=1; continue;
}
for(k=0;k<4;k++){
int xx=i+dx[k];
int yy=j+dy[k];
if(Ok(xx,yy,1)){
cnt++; a[C(i,j)][C(xx,yy)]=1;
}
}
a[C(i,j)][C(i,j)]=-1*cnt;
x[C(i,j)]=-1*cnt;
}
}
int Gauss(){
int i,j,k,col,max_r; //max_r 指现在对哪一行操作 equ 方程数 var 未知数个数
for(k=0,col=0;k<equ && col<var;k++,col++){
max_r=k;
for(i=k+1;i<equ;i++)
if(fabs(a[i][col])>fabs(a[max_r][col])) max_r=k; //寻找这个未知数最大的一个
if(fabs(a[max_r][col])<eps) {
if(col==C(sx,sy)) return 0;
else continue;
}
if(k!=max_r){
for(j=col;j<var;j++) swap(a[k][j],a[max_r][j]);
swap(x[k],x[max_r]);
}
x[k]/=a[k][col];
for(j=col+1;j<var;j++) a[k][j]/=a[k][col];
a[k][col]=1;
for(i=0;i<equ;i++)
if(i!=k){
x[i]-=x[k]*a[i][col];
for(j=col+1;j<var;j++) a[i][j]-=a[k][j]*a[i][col];
a[i][col]=0;
}
}
return 1;
} int main(){
//freopen("in.txt","r",stdin);
int i,j,k;
while(~scanf("%d %d",&n,&m)){
q.empty();
memset(vis,0,sizeof(vis));
for(i=0;i<n;i++) scanf("%s",mp[i]);
for(i=0;i<n;i++)
for(j=0;j<m;j++){
if(mp[i][j]=='@'){ sx=i; sy=j; }
if(mp[i][j]=='$'){ aa.x=i; aa.y=j; q.push(aa); vis[i][j]=1; }
}
Bfs();
memset(a,0,sizeof(a));
memset(x,0,sizeof(x));
Makefunction();
var=n*m; equ=n*m;
;
if(vis[sx][sy] && Gauss() ) printf("%lf\n",x[C(sx,sy)]);
else printf("-1\n");
}
return 0;
}
hdu2262 Where is the canteen的更多相关文章
- HDU-2262 Where is the canteen 概率DP,高斯消元
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2262 题意:LL在一个迷宫里面转,每次走向周围能走的点的概率都是一样的,现在LL要随机的走到cante ...
- HDU2262;Where is the canteen(高斯消元+期望)
传送门 题意 给出一张图,LL从一个点等概率走到上下左右位置,询问LL从宿舍走到餐厅的步数期望 分析 该题是一道高斯消元+期望的题目 难点在于构造矩阵,我们发现以下结论 设某点走到餐厅的期望为Ek 1 ...
- 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 ...
- unknown directive "" in E:\canteen\nginx-1.16.0/conf/nginx.conf:3-------文本编辑器修改nginx配置文件的坑
nignx小白一个,今天在配置nginx的时候,理所当然的用了文本编辑器编辑并保存了一下nginx的nginx.conf配置文件,一不小心就折腾了几个钟. 保存之后就nginx -s reload一下 ...
- 1350. Canteen(map)
1350 这题没什么 就考一下map的用法吧 #include <iostream> #include<cstdio> #include<cstring> #in ...
- hdu2262 高斯消元
题目:有一个地图,一个人从某个点出发,问走到花园的期望步数为多少 设某点的期望步数为Ei. 那么目标的Ei=0. Ei=(Enext1+Enext2……Enextk)/k+1. 为什么是这个公式 因为 ...
- SQLServer------Join的使用方法
参考菜鸟教程网: http://www.runoob.com/sql/sql-join.html select a.Canteen,b.OrderNum,b.CreateTime,c.Name fro ...
- IELTS - Word List 28
1, The lawsuit is very much o the lawyer's mind. 2, The canteen was absolutely packed. 3, Doctors di ...
- Do things for others
早上,按照平常的时间去吃早饭,食堂格外的空旷,打饭的员工说今天人很少,我说昨天是有元旦晚会,她说今天是放假,我后来想,还是她说的更有道理.她看的比我清楚更清楚! 幸亏昨晚上记录下了早上要帮别人搜论文的 ...
随机推荐
- RAID知识总结[转]
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://815632410.blog.51cto.com/1544685/1394306 ...
- jquery参考手册
开始使用 jQueryjQuery 本身只有一个 js 文件,所以,要使用它,就和使用其它的 js 文件一样,直接将它引入就可以使用了. <script type="text/java ...
- Burp_用户名密码爆破
burp 全称 Burp Suite, 是用于攻击web 应用程序的集成平台.它包含了许多工具,可以抓包可以爆破也可以扫描漏洞. 主要组件如下: Proxy--是一个拦截HTTP/S的代理服务器,作为 ...
- centos7安装部署gitlab服务器
[gitlab需要内存至少4GB] 我这里使用的是centos 7 64bit,我试过centos 6也是可以的! 1. 安装依赖软件 yum -y install policycoreutils ...
- ★Linux桌面系统技巧(作为客户端)
[安装chrome浏览器]* 下载(已下载完成):32位:wget https://dl.google.com/linux/direct/google-chrome-stable_current_i3 ...
- iOS实现微信外部H5支付完成后返回原APP
看到微信最近放开了微信H5支付,公司决定把H5集成到多款APP上.下面记录下了开发过程. 由于是微信新推出的支付方式,在网上搜索到的相关资料并不多,其中有一篇文件(点此跳转)对我的整个开发过程起到了很 ...
- Java经典编程题50道之四十五
判断一个整数能被几个9整除. public class Example45 { public static void main(String[] args) { f(729); ...
- shell 颜色控制系列
shell脚本里,经常用的颜色控制,如下 格式:echo -e "\033[字背景颜色:文字颜色m字符串\033[0m" eg:echo -e "\033[41;36m ...
- c++ 如何获取多线程的返回值?
// Console.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <stdlib.h> #include ...
- UVA1213
先打表,再回溯+剪枝 AC代码: #include<cstdio> #include<cstring> #include<cmath> const int maxn ...