HDOJ3085 Nightmare II 双向BFS
重构一遍就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
HDOJ3085 Nightmare II 双向BFS的更多相关文章
- HDU 3085 Nightmare II 双向bfs 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...
- Nightmare Ⅱ(双向BFS)
Problem Description Last night, little erriyue had a horrible nightmare. He dreamed that he and his ...
- HDU 3085 Nightmare Ⅱ(双向BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 题目大意:给你一张n*m地图上,上面有有 ‘. ’:路 ‘X':墙 ’Z':鬼,每秒移动2步,可 ...
- HDU 3085 Nightmare Ⅱ (双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU3085 Nightmare Ⅱ —— 双向BFS + 曼哈顿距离
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Other ...
- HDU 3085 Nightmare Ⅱ 双向BFS
题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...
- HDU3085 Nightmare Ⅱ (双向BFS)
联赛前该练什么?DP,树型,状压当然是爆搜啦 双向BFS就是两个普通BFS通过一拼接函数联系,多多判断啦 #include <iostream> #include <cstdio&g ...
- Word Ladder(双向BFS)
2018-10-02 23:46:38 问题描述: 问题求解: 显然是个解空间遍历问题,每次修改其中一位,由于步长是1,所以可以使用BFS进行解空间的遍历.
- HDU3085(双向BFS+曼哈顿距离)题解
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
随机推荐
- 分布式缓存产品Redis和memcached比较区别(图)
- win7安装linux双系统
整体流程大概就是下载启动盘制作工具以及linux镜像,这些步骤网上很多,我就不再重复了 这里以centos举例说几个我踩到的坑吧 1.选择开始安装后提示 Warning: /dev/root does ...
- SaeMysql操作示例
新浪sae的官方说明文档:http://apidoc.sinaapp.com/sae/SaeMysql.html Class SaeMysql 具体实现:http://apidoc.sinaapp.c ...
- 50. Pow(x, n) 幂次方
[抄题]: mplement pow(x, n), which calculates x raised to the power n (xn). Example 1: Input: 2.00000, ...
- linux Shell中常用的条件判断
linux Shell中常用的条件判断 -b file 若文件存在且是一个块特殊文件,则为真 -c file 若文件存在且是一个字符特殊文件,则为真 -d ...
- 关于wamp中升级PHP+Apache 的问题
首先个人不建议wamp中升级php版本,如果你不信可以试一试,当你php升级后发想,奥,Apache版本不匹配,然后又去升级Apache,结果搞了半天,弄出来了就好,要是没出来,可能你会气死(好吧,气 ...
- spring.net 集成nhibernate配置文件(这里暴露了GetCurrentSession 对于 CurrentSession unbond thread这里给出了解决方法)
我这里主要分成了两个xml来进行spring.net管理实际情况中可自己根据需要进行分类 Dao2.xml <?xml version="1.0" encoding=&quo ...
- Reportng 的测试报告在 Jenkins 中显示不全
通过Jenkins执行接口测试生成测试报告,用Jenkins的web服务打开html显示不全. 环境: Jenkins版本:1.651.2 Jenkins发布在mac上面 使用的是testng管理测试 ...
- wireshark抓取qq数据包
抓包接口设置成本地连接 点击start,登录qq,输入oicq进行过滤qq包 找到第一个OICQ,点击后,点击oicq-IM software 可以看到自己登录的QQ号码为765343409 本机IP ...
- 如何创建JUnit
这里拿Dynamic项目来演示,首先创建一个Dynamic项目,起名,点next, 继续点next, 将web.xml文件勾选,finish, 接下来在Java Resources->src下创 ...