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 Ⅱ的更多相关文章

  1. HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ)

    HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  2. HDU 3085 Nightmare II 双向bfs 难度:2

    http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...

  3. HDU 3085 Nightmare Ⅱ(双向BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 题目大意:给你一张n*m地图上,上面有有 ‘. ’:路 ‘X':墙 ’Z':鬼,每秒移动2步,可 ...

  4. HDU 3085 Nightmare Ⅱ (双向BFS)

    Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  5. HDU 3085 Nightmare Ⅱ 双向BFS

    题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...

  6. [hdu P3085] Nightmare Ⅱ

    [hdu P3085] Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...

  7. 【HDU 3085】 Nightmare Ⅱ

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3085 [算法] 双向BFS [代码] #include<bits/stdc++.h> ...

  8. Nightmare Ⅱ HDU - 3085 (双向bfs)

    Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were tra ...

  9. 【HDU - 3085】Nightmare Ⅱ(bfs)

    -->Nightmare Ⅱ 原题太复杂,直接简单的讲中文吧 Descriptions: X表示墙 .表示路 M,G表示两个人 Z表示鬼 M要去找G但是有两个鬼(Z)会阻碍他们,每一轮都是M和G ...

随机推荐

  1. 滑动窗口的最大值 · sliding-window-maximum

    [抄题]: Given an array nums, there is a sliding window of size k which is moving from the very left of ...

  2. [udemy]WebDevelopment_CSS

    Your First CSS(Cascading Style Sheets) Cascading means it always takes selector that is at the end 即 ...

  3. java高级工程师(二)

    一.Java底层基础题 1.SpringMVC的原理以及返回数据如何渲染到jsp/html上? 答:Spring MVC的核心就是 DispatcherServlet , 一个请求经过 Dispatc ...

  4. How To Check Member In Window VS With CplusPlus?

    实例说明 下面这个实例代码, 快速举例了在Win32应用程序下,对于内存的泄漏检查. 其中的原理,目前本人还是不太的理解. 只是记录了使用方法. 以后,看情况,会更新的. #ifdef _WIN32 ...

  5. 链接ftp,把文件或图片上传到ftp指定的文件夹中

    /******************************************************************** *  * * Filename : .java * Auth ...

  6. java 框架收藏

    一.java 异步非阻塞编程框架 1.Spring Webflux 2.Vert.x 3.Ratpack 4.smart-socket 国产异步框架 二.微服务框架 1.Jboot :专为大型分布式项 ...

  7. Laravel 配置文件操作方法

    1)laravel 的所以配置文件都在根目录下的 config 目录里,直接看一个配置文件的名字就知道是做什么的了,这里不说了 2)读取配置的方法 $value = config('app.timez ...

  8. pyspider示例代码三:用PyQuery解析页面数据

    本系列文章主要记录和讲解pyspider的示例代码,希望能抛砖引玉.pyspider示例代码官方网站是http://demo.pyspider.org/.上面的示例代码太多,无从下手.因此本人找出一些 ...

  9. 数据结构(c语言版)文摘

    第一章  绪论 数据结构:是一门研究非数值计算的程序设计问题中计算机的操作对象以及它们之间的关系和操作等的学科. 数据:是对客观事物的符号表示,在计算机科学中是指所有能输入到计算机中并被计算机程序处理 ...

  10. win7搭建node+npm+bower的环境

    原文的地址:https://my.oschina.net/JeeChou/blog/219699 Windows下的NodeJS安装是比较方便的(v0.6.0版本之后,支持windows native ...