HDU 3085 Nightmare Ⅱ (双向BFS)
Nightmare Ⅱ
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 886 Accepted Submission(s): 185
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std; const int SIZE = ;
const int UPDATE[][] = {{-,},{,},{,-},{,}};
int N,M;
int STEP;
int MAP[SIZE][SIZE];
int VIS_1[SIZE][SIZE],VIS_2[SIZE][SIZE];
int Z_X_1,Z_Y_1,Z_X_2,Z_Y_2; struct Node
{
int x,y;
bool check(void)
{
if(x >= && x <= N && y >= && y <= M && MAP[x][y] != 'X'
&& (abs(Z_X_1 - x) + abs(Z_Y_1 - y) > * STEP)
&& (abs(Z_X_2 - x) + abs(Z_Y_2 - y) > * STEP))
return true;
return false;
}
}; int dbfs(Node boy,Node girl);
int main(void)
{
int t,ans,flag;
Node boy,girl;
char s[SIZE]; scanf("%d",&t);
while(t --)
{
flag = ;
scanf("%d%d",&N,&M);
getchar();
for(int i = ;i <= N;i ++)
{
scanf("%s",&s[]);
for(int j = ;j <= M;j ++)
{
MAP[i][j] = s[j];
if(MAP[i][j] == 'M')
{
boy.x = i;
boy.y = j;
}
else if(MAP[i][j] == 'G')
{
girl.x = i;
girl.y = j;
}
else if(MAP[i][j] == 'Z' && flag)
{
Z_X_1 = i;
Z_Y_1 = j;
flag = ;
}
else if(MAP[i][j] == 'Z')
{
Z_X_2 = i;
Z_Y_2 = j;
}
}
} ans = dbfs(boy,girl);
printf("%d\n",ans);
} return ;
} int dbfs(Node boy,Node girl)
{
memset(VIS_1,,sizeof(VIS_1));
memset(VIS_2,,sizeof(VIS_2));
VIS_1[boy.x][boy.y] = ;
VIS_2[girl.x][girl.y] = ;
STEP = ; queue<Node> que_boy,que_girl;
que_boy.push(boy);
que_girl.push(girl); while()
{
for(int i = ;i < ;i ++)
{
int size = que_boy.size();
while(size --)
{
Node old = que_boy.front();
que_boy.pop();
if(!old.check())
continue; for(int j = ;j < ;j ++)
{
Node cur = old; cur.x += UPDATE[j][];
cur.y += UPDATE[j][];
if(!cur.check() || VIS_1[cur.x][cur.y])
continue;
if(VIS_2[cur.x][cur.y])
return STEP; VIS_1[cur.x][cur.y] = ;
que_boy.push(cur);
}
}
} if(que_girl.empty() && que_boy.empty())
return -;
if(que_girl.empty())
continue; int size = que_girl.size();
while(size --)
{
Node old = que_girl.front();
que_girl.pop();
if(!old.check())
continue; for(int j = ;j < ;j ++)
{
Node cur = old; cur.x += UPDATE[j][];
cur.y += UPDATE[j][];
if(!cur.check() || VIS_2[cur.x][cur.y])
continue;
if(VIS_1[cur.x][cur.y])
return STEP; VIS_2[cur.x][cur.y] = ;
que_girl.push(cur);
}
}
STEP ++;
} return -;
}
HDU 3085 Nightmare Ⅱ (双向BFS)的更多相关文章
- HDU 3085 Nightmare Ⅱ 双向BFS
题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...
- 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 Ⅱ
HDU - 3085 Nightmare Ⅱ 双向BFS,建立两个队列,让男孩女孩一起走 鬼的位置用曼哈顿距离判断一下,如果该位置与鬼的曼哈顿距离小于等于当前轮数的两倍,则已经被鬼覆盖 #includ ...
- 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步,可 ...
- HDU3085 Nightmare Ⅱ —— 双向BFS + 曼哈顿距离
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Other ...
- 2017多校第10场 HDU 6171 Admiral 双向BFS或者A*搜索
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6171 题意: 给你一个高度为6的塔形数组,你每次只能将0与他上下相邻的某个数交换,问最少交换多少次可以 ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
随机推荐
- POJ 1236 Network of Schools (有向图的强连通分量)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9073 Accepted: 359 ...
- [iOS UI进阶 - 2.4] 彩票Demo v1.4 转盘动画
A.需求 幸运广场界面中有一个幸运转盘,平时能够自动缓缓转动 能够选择星座 点击“开始选号”开速旋转转盘,旋转一定周数 转盘转动速度节奏:开始-慢-块-慢-结束 设置其余的背景和按钮 code s ...
- Codeforces Round #201 (Div. 2) - C. Alice and Bob
题目链接:http://codeforces.com/contest/347/problem/C 题意是给你一个数n,然后n个数,这些数互不相同.每次可以取两个数x和y,然后可以得到|x - y|这个 ...
- 计算两个日期相隔的天数(jodd)
public static void main(String[] args) throws ParseException { System.out.println(TimeUtil.dayOfYear ...
- 启动报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
如果你是maven项目,tomcat在发布项目的时候没有同时发布maven依赖所添加的jar包,你需要设置一下eclipse:项目 —> 属性 -> Deployment Assembly ...
- libevent的使用方法--回显服务器的简单实例
#include <event.h> #include <sys/types.h> #include <sys/socket.h> #include <net ...
- Castle IOC容器快速入门
主要内容 1.为什么要IOC 2.什么是Castle IOC容器 3.快速入门示例 4.几个重要的概念 一,为什么要IOC IOC(控制反转或者叫依赖注入)Martin Fowler大师在他的文章中已 ...
- Js面向对象和数据类型内存分配(转)
一 Js基本数据类型以及内存情况 1 Undefined Undefined类型只有一个值undefined,在使用了声明但未初始化的变量的时候,这个变量值就是undefined 1 var hi; ...
- JS Math 类库介绍
下面介绍下随机生成数的常用几个API JS 随机数生成 : 在JavaScript , 提供了生成随机数的API, Math.random() 1.Math.random() : 随机生成小数 . 生 ...
- Python3爬取百度百科(配合PHP)
用PHP写了一个网页,可以获取百度百科词条.源代码已分享至github:https://github.com/1049451037/xiaobaike/tree/master 那么通过Python来爬 ...