首页 技术 正文
技术 2022年11月23日
0 收藏 400 点赞 2,255 浏览 1806 个字

重构一遍就A了。。。但这样效率太低了。。。莫非都要重构???QWQ


每一秒男同志bfs3层,女同志bfs1层。注意扩展状态时,要判一下合不合法再扩展,而不是只判扩展的状态合不合法,否则有可能由非法的走到合法的地方。

#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
#define R register int
const int dx[]={-,,,},dy[]={,,,-};
using namespace std;
inline int g() {
R ret=,fix=; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-:fix;
do ret=ret*+(ch^); while(isdigit(ch=getchar())); return ret*fix;
}
int n,m,t,tim,cc;
int gx,gy,mx,my,zx,zy,zx1,zy1;
char e[][];
inline int abs(int x) {return x>?x:-x;}
inline bool is(char ch) {return ch=='X'||ch=='Z'||ch=='M'||ch=='G'||ch=='.';}
inline bool ckpos(int x,int y) {
if(x<||x>n||y<||y>m||e[x][y]=='X') return false;
if(*tim>=abs(x-zx)+abs(y-zy)||*tim>=abs(x-zx1)+abs(y-zy1)) return false;
return true;
}
struct node{int x,y; node() {}
node(int xx,int yy) {x=xx,y=yy;}
};
queue<node>q,q1;
inline bool bfs() {
for(R x=;x<=;++x) { queue<node> qt=q; R sz=q.size();
while(sz--) {
node u=qt.front(); qt.pop(); R x=u.x,y=u.y;
if(!ckpos(x,y)) continue;
for(R i=;i<;++i) {
R xx=x+dx[i],yy=y+dy[i];
if(!ckpos(xx,yy)||e[xx][yy]=='M') continue;
if(e[xx][yy]=='G') return true; e[xx][yy]='M';
qt.push(node(xx,yy));
}
} q=qt;
} return false;
}
inline bool bfs1() { queue<node> qt=q1; R sz=q1.size();
while(sz--) {
node u=qt.front(); qt.pop(); R x=u.x,y=u.y;
if(!ckpos(x,y)) continue;
for(R i=;i<;++i) {
R xx=x+dx[i],yy=y+dy[i];
if(!ckpos(xx,yy)||e[xx][yy]=='G') continue;
if(e[xx][yy]=='M') return true; e[xx][yy]='G';
qt.push(node(xx,yy));
}
} q1=qt; return false;
}
signed main() {
t=g(); while(t--) { tim=; cc=;
n=g(),m=g();
for(R i=;i<=n;++i) for(R j=;j<=m;++j) {
while(!is(e[i][j]=getchar())); if(e[i][j]=='M') mx=i,my=j;
else if(e[i][j]=='G') gx=i,gy=j;
else if(e[i][j]=='Z') if(cc) zx1=i,zy1=j; else zx=i,zy=j,++cc;
} while(q.size()) q.pop(); while(q1.size()) q1.pop();
q.push(node(mx,my)); q1.push(node(gx,gy));
while(++tim) {
if(bfs()||bfs1()) {printf("%d\n",tim); break;}
if(q1.empty()||q.empty()) {printf("-1\n"); break;}
}
}
}

2019.04.27

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,996
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,510
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,353
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,137
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848