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. SQL Server判断数据库、表、存储过程、函数是否存在

    --判断数据库是否存在 if exists (select * from sys.databases where name = '数据库名') drop database [数据库名] --判断表是否 ...

  2. foo是什么?

    在英文程序员社区里,foo bar baz这些词是常用于作为占位符而使用的.要查wiki的话,这项更合适:http://en.wikipedia.org/wiki/Metasyntactic_vari ...

  3. MyBatis 插入主键方式和返回主键

    这使用的mysql数据库,下面这种方式(没有给mysql设置自动增长)是插入主键方式: <insert id="insertBook" parameterType=" ...

  4. metasploitable实践

    使用Fimap和metasploitable2文件包含漏洞测试 fimap 首先查看msf已经存在的漏洞: root@kali:~# fimap -u 'http://192.168.136.130/ ...

  5. [转]程序集之GAC---Global Assembly Cache

    本文转自:http://www.cnblogs.com/jhxk/articles/2564295.html 1.什么是GAC?GAC解决什么问题? GAC全称为: Global Assembly C ...

  6. Spring 之 IOC

    IoC的全称是Inversion of Control,中文称为控制反转, Martin Flower由根据它创造了一个新词:Dependency Injection,中文称为依赖注入.这两个词讲的是 ...

  7. Reverting back to the R12.1.1 and R12.1.3 Homepage Layout

    Reverting back to the 12.1.1 Homepage Layout Set the following profiles: FND: Applications Navigator ...

  8. ASP.NET Core2实现静默获取微信公众号的用户OpenId

    最近在做个微信公众号的项目,需要将入口放置在公众号二级菜单内,通过点击该菜单链接后进入到该项目中去,进入到项目后程序会自动通过微信公众号的API完成用户的OpenId获取.需求很简单,实现起来也不复杂 ...

  9. 基于Extjs的web表单设计器 第一节

    前面一节介绍了表单设计器的背景和最终的大概样式,本节主要介绍表单设计器的需求及功能设计. 在讲需求之前先明确几个常用的概念: 主表或者卡片表——具有多行多列的一个区域的控件块,如下图所示. 明细表—— ...

  10. 比较git commit 两个版本之间次数

    #!/bin/bash f1="$1*" f2="$2*" echo "第一个版本:"$f1 echo "第二个版本:" ...