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 ...
随机推荐
- 给我们的Empty Object加个图标
Gizmos.DrawIcon (transform.position, "1.png", true);
- 接雨水12 · Trapping Rain Water12
[抄题]: Given n non-negative integers representing an elevation map where the width of each bar is 1, ...
- DART: a fast and accurate RNA-seq mapper with a partitioning strategy DART:使用分区策略的快速准确的RNA-seq映射器
DART: a fast and accurate RNA-seq mapper with a partitioning strategyDART:使用分区策略的快速准确的RNA-seq映射器 Abs ...
- qt5.7 安装
http://blog.csdn.net/liang19890820/article/details/53931813#安装-qt57 安装运行出错:qt vstool 指定qt安装路径 http:/ ...
- [Training Video - 1] [Introduction to Web services]
What is a web service? http://webservicex.net/ws/default.aspx WebService是一种跨编程语言和跨操作系统平台的远程调用技术. 跨编程 ...
- HTML5 桌面消息提醒
Notification HTML5新属性,复制代码创建HTML文件,浏览器查看效果 <!DOCTYPE html> <html lang="en"> &l ...
- win10自带虚拟机安装CentOS7系统(转)
出处:http://blog.csdn.net/bimabushihaodongxi/article/details/53677490 话说工欲善其事,必先利其器,在我准备学习Linux之前先要完成l ...
- redis 工具
搜索关键词:redis 工具 推荐:http://redisdesktop.com/download 点评:使用顺手,顺畅. ★★★★★ 五星好评 备注:redis key 可以采用某种format ...
- C/C++编程可用的Linux自带工具
GNU Binary Utilities或binutils是一整套的编程语言工具程序,用来处理许多格式的目标文件.当前的版本原本由在Cygnus Solutions的程序员以Binary File D ...
- Oracle EBS Request Status: Pending
如果提交请求以后,状态一直是pending状态,可以在"工具"打开"Manager",查看一下Maximum是否有设置错,另外pending的数量当前是多少. ...