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. Linux Terminal Games

    linux Terminal Games install note:sudo apt install, or sudo apt-get install 2048 // download c src # ...

  2. datagridview paging

    http://www.codeproject.com/Articles/211551/A-Simple-way-for-Paging-in-DataGridView-in-WinForm

  3. 洛谷 P1260 工程规划(差分约束)

    题目描述 造一幢大楼是一项艰巨的工程,它是由n个子任务构成的,给它们分别编号1,2,…,n(5≤n≤1000).由于对一些任务的起始条件有着严格的限制,所以每个任务的起始时间T1,T2,…,Tn并不是 ...

  4. 从YouTube改版看“移动优先”——8个移动优先网站设计案例赏析

    2011年,Luke Wroblewski大神提出了移动优先的设计理念.在当时看来这无疑是一个打破行业常规的新型设计原则.而在移动互联网大行其道的今天,谁遵守移动优先的设计理念,设计出最好的移动端网站 ...

  5. JavaScript 对象属性作实参以及实参对象的callee属性

    参考自<<JavaScript权威指南 第6版>> /* * 将对象属性用作实参, 从而不必记住参数的顺序. */ function arraycopy(from,from_s ...

  6. Linux系统下安装ncurses库

    ncurses库是一个Linux系统下的图形支持的函数库,字符终端处理库,包括面板和菜单. 今天在安装ncurses库的时候遇到了一些问题,现将遇到的问题所叙如下: 首先说明:本次安装采用的是源码包的 ...

  7. UIWebView清除缓存和cookie[转]

    现在项目遇到一个问题,游戏底层用Cocos2d-x,公告UI实现是用的UIWebView, 然后第一次在有网络的环境下运行公告UI,会加载url链接,同时就会自动存入缓存,当下次手机没有网络的环境下, ...

  8. php的循环与引用的一个坑

    上代码 $arr = array( 'a'=> 'a11', 'b'=> 'b22', 'c'=> 'c33', ); foreach ($arr as $k=>&$v ...

  9. [Erlang24]使用zotonic搭建网站记录

    zotonic的搭建网站(blog)记录:   zotonic:用Erlang做的一个web 框架:   和wordpress 类似,但是官网称比PHP CMS要快10倍以上   先看看我的成果:正弦 ...

  10. Selenium下拉菜单(Select)的操作-----Selenium快速入门(五)

    对于一般元素的操作,我们只要掌握本系列的第二,三章即可大致足够.对于下拉菜单(Select)的操作,Selenium有专门的类Select进行处理.文档地址为:http://seleniumhq.gi ...