HDU - 3085 Nightmare Ⅱ
HDU - 3085 Nightmare Ⅱ
双向BFS,建立两个队列,让男孩女孩一起走
鬼的位置用曼哈顿距离判断一下,如果该位置与鬼的曼哈顿距离小于等于当前轮数的两倍,则已经被鬼覆盖
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <cstring>
using namespace std; #define res register int
const int N=+;
const int dx[]={,-,,},dy[]={,,,-};
int n,m;
char s[N][N];
bool vis[N][N][];
struct node{
int x,y;
node() {};
node(int x,int y) : x(x),y(y) {}
}; queue<node> q[];
node mm,gg,gho[]; inline int _dis(int a,int b,int x,int y)
{
return abs(a-x)+abs(b-y);
}
inline bool check(int x,int y) {
if(x< || x>n || y< || y>m || s[x][y]=='X') return false;
else return true;
}
inline bool check2(int x,int y,int stp)
{
for(res i= ; i<= ; i++)
if(_dis(x,y,gho[i].x,gho[i].y)<=*stp) return false;
return true;
} inline bool bfs(int typ,int stp)
{
int len=q[typ].size();
while(len--)
{
node u=q[typ].front(); q[typ].pop();
if(!check(u.x,u.y) || !check2(u.x,u.y,stp)) continue;
for(res i= ; i< ; i++)
{
int nx=u.x+dx[i],ny=u.y+dy[i];
if(!check(nx,ny) || !check2(nx,ny,stp)) continue;
if(vis[nx][ny][typ^]) return true;
if(vis[nx][ny][typ]) continue;
vis[nx][ny][typ]=;
q[typ].push(node(nx,ny));
}
}
return false; } inline void init()
{
while(q[].size()) q[].pop();
while(q[].size()) q[].pop();
memset(vis,,sizeof(vis));
} inline int run()
{
init();
vis[mm.x][mm.y][]=;
vis[gg.x][gg.y][]=;
q[].push(node(mm.x,mm.y));
q[].push(node(gg.x,gg.y));
int stp();
while(q[].size() || q[].size())
{
stp++;
for(res i= ; i<= ; i++)
if(bfs(,stp)) return stp;
if(bfs(,stp)) return stp;
}
return -;
} int main()
{
int T ; scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
for(res i= ; i<=n ; i++) scanf("%s",s[i]+);
int t();
for(res i= ; i<=n ; i++)
for(res j= ; j<=m ; j++)
if(s[i][j]=='G') {
gg.x=i,gg.y=j;
}
else if(s[i][j]=='M') {
mm.x=i,mm.y=j;
}
else if(s[i][j]=='Z') {
gho[t].x=i,gho[t++].y=j;
} printf("%d\n",run());
}
return ;
}
my code
HDU - 3085 Nightmare Ⅱ的更多相关文章
- HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ)
HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- HDU 3085 Nightmare II 双向bfs 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...
- 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 ...
- HDU 3085 Nightmare Ⅱ 双向BFS
题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...
- [hdu P3085] Nightmare Ⅱ
[hdu P3085] Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 【HDU 3085】 Nightmare Ⅱ
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3085 [算法] 双向BFS [代码] #include<bits/stdc++.h> ...
- Nightmare Ⅱ HDU - 3085 (双向bfs)
Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were tra ...
- 【HDU - 3085】Nightmare Ⅱ(bfs)
-->Nightmare Ⅱ 原题太复杂,直接简单的讲中文吧 Descriptions: X表示墙 .表示路 M,G表示两个人 Z表示鬼 M要去找G但是有两个鬼(Z)会阻碍他们,每一轮都是M和G ...
随机推荐
- 从值栈获取List集合
-------------------siwuxie095 从值栈获取 List 集合 1.具体步骤 (1)在 Action 中向值栈放 List 集合 (2)在 JSP 页面中从值栈获取 List ...
- 8-linux 安装 requests 时 pip install 安装不了
安装提示更新:但是必须要sudo才行: sudo pip install --upgrade pip 安装 requests时有报错:这样写可以: sudo python -m pip install ...
- Qt的安装和使用中的常见问题(简略版)
对于喜欢研究细节的朋友,可参考Qt的安装和使用中的常见问题(详细版). 目录 1.引入 2.Qt简介 3.Qt版本 3.1 查看安装的Qt版本 3.2 查看当前项目使用的Qt版本 3.3 查看当前项目 ...
- jave 逻辑运算 vs 位运算 + Python 逻辑运算 vs 位运算
JAVA中&&和&.||和|(短路与和逻辑与.短路或和逻辑或)的区别 博客分类: 面试题目 Java.netBlog 转自 :http://blog.csdn.net/web ...
- jquery对象的遍历$(selector).each()
<!DOCTYPE html> <html> <head> <script language="javascript" src=" ...
- mongo学习- 副本集 大多数原则
副本集中有一个重要的概念“大多数”,意思是说,选择主节点需要大多数决定(本人亲自做了实验) 步骤: 1.开启副本集(如果没有配置好 副本集的 亲参考我的上篇文章 https://www.cnblog ...
- 【工具推荐】ELMAH——可插拔错误日志工具(转)
出处:http://www.cnblogs.com/liping13599168/archive/2011/02/23/1962625.html 今天看到一篇文章(构建ASP.NET网站十大必备工具( ...
- 20169205实验四 Android程序设计
20169205实验四 Android程序设计 实验内容及步骤 (一)第一个android studio项目 android studio与eclipse的传统安卓开发有一些不同之处 android ...
- 如何将图片嵌入到Html中
将图片内嵌入到Html中,最好的方法就是用Base64 string.例如:<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUg ...
- centos下安装pip2
# 背景 新机器,安装完python2后发现竟然不自带pip,按照我的理解现在新版本的python,不管是2还是3都会自带pip的.没办法,需要自己去安装pip的 # 步骤 1. 最开始查到的是通过y ...