BFS:HDU3085-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
Problem Description
his girl friend before the ghosts find them.
You may suppose that little erriyue and his girl friend can move in 4 directions. In each second, little erriyue can move 3 steps and his girl friend can move 1 step. The ghosts are evil, every second they will divide into several parts to occupy the grids
within 2 steps to them until they occupy the whole maze. You can suppose that at every second the ghosts divide firstly then the little erriyue and his girl friend start to move, and if little erriyue or his girl friend arrive at a grid with a ghost, they
will die.
Note: the new ghosts also can devide as the original ghost.
Each test case starts with a line contains two integers n and m, means the size of the maze. (1<n, m<800)
The next n lines describe the maze. Each line contains m characters. The characters may be:
‘.’ denotes an empty place, all can walk on.
‘X’ denotes a wall, only people can’t walk on.
‘M’ denotes little erriyue
‘G’ denotes the girl friend.
‘Z’ denotes the ghosts.
It is guaranteed that will contain exactly one letter M, one letter G and two letters Z.
5 6
XXXXXX
XZ..ZX
XXXXXX
M.G...
......
5 6
XXXXXX
XZZ..X
XXXXXX
M.....
..G...
10 10
..........
..X.......
..M.X...X.
X.........
.X..X.X.X.
.........X
..XX....X.
X....G...X
...ZX.X...
...Z..X..X
1
-1
n*m地图上有
‘. ’:路
‘X':墙
’Z':鬼,每秒蔓延2个单位长度,可以穿墙,共两个,每秒开始时鬼先动
‘M’:一号,每分钟可移动3个单位长度
‘G’:二号,每分钟课移动1个单位长度
#include<bits/stdc++.h>
using namespace std;
const int maxn = 880;
int n,m;
int dir[4][2] = {1,0,-1,0,0,1,0,-1};
char maps[maxn][maxn];
struct node
{
int x,y;
} now,Next,b,g,z[2];
queue <node> q[2],qt;//q[0]为boy,q[1]为girl,qt为当前操作的队列
int step = 0;
void pre_maps()
{
//多组输入,清零
while(!q[0].empty())
q[0].pop();
while(!q[1].empty())
q[1].pop();
while(!qt.empty())
qt.pop(); int k = 0;
for(int i=0; i<n; i++)
scanf("%s",maps[i]);
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
{
if(maps[i][j] == 'Z')
{
z[k].x = i;
z[k].y = j;
k++;
}
if(maps[i][j] == 'M')
{
b.x = i;
b.y = j;
}
if(maps[i][j] == 'G')
{
g.x = i;
g.y = j;
}
}
} bool check(node a)
{
if(a.x <0 || a.y <0 || a.x >= n || a.y >= m)//超出地图直接返回
return true;
for(int i=0; i<2; i++)
{
if(maps[a.x][a.y] == 'X' || (abs(a.x-z[i].x)+abs(a.y-z[i].y)) <= 2*step)//是墙,或者被鬼抓到返回
return true;
}
return false;
}
bool bfs(int k,int num,char start,char End)
{
qt = q[k];//用来表示当前层的搜索,队列也可以直接赋值
for(int i=0; i<num; i++)
{
while(!qt.empty())
{
now = qt.front();
qt.pop();
q[k].pop();
if(check(now)) continue;
for(int j=0; j<4; j++)
{
Next.x = now.x + dir[j][0];
Next.y = now.y + dir[j][1];
if(check(Next)) continue;
if(maps[Next.x][Next.y] == start) continue;//已经走过了
if(maps[Next.x][Next.y] == End)//两个bfs相遇
return true;
maps[Next.x][Next.y] = start;//走过的地点直接标记
q[k].push(Next);
}
}
qt = q[k];//将下一层的队列赋值给当前层
}
return false;
} int solve()
{
bool flag1 = false,flag2 = false;
step = 0;
q[0].push(b);
q[1].push(g);
while(!q[0].empty() && !q[1].empty())
{
step++; flag1 = bfs(0,3,'M','G');//boy的bfs
flag2 = bfs(1,1,'G','M');//girl的bfs
if(flag1 || flag2)//当有一方相遇了
return step;
}
return -1;//没有找到
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
pre_maps();
printf("%d\n",solve());
}
}
BFS:HDU3085-Nightmare Ⅱ(双向BFS)的更多相关文章
- HDU3085 Nightmare Ⅱ —— 双向BFS + 曼哈顿距离
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Other ...
- HDU3085 Nightmare Ⅱ (双向BFS)
联赛前该练什么?DP,树型,状压当然是爆搜啦 双向BFS就是两个普通BFS通过一拼接函数联系,多多判断啦 #include <iostream> #include <cstdio&g ...
- POJ 1915-Knight Moves (单向BFS && 双向BFS 比)
主题链接:Knight Moves 题意:8个方向的 马跳式走法 ,已知起点 和终点,求最短路 研究了一下双向BFS,不是非常难,和普通的BFS一样.双向BFS只是是从 起点和终点同一时候開始搜索,可 ...
- HDU 3085 Nightmare Ⅱ (双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU3085(双向BFS+曼哈顿距离)题解
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 3085 Nightmare Ⅱ 双向BFS
题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...
- HDU3085(KB2-G 双向bfs)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU——1195Open the Lock(双向BFS)
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- Nightmare Ⅱ(双向BFS)
Problem Description Last night, little erriyue had a horrible nightmare. He dreamed that he and his ...
随机推荐
- Android 中文 API (101) —— AsyncTask
一.结构 public abstract class AsyncTask extends Object java.lang.Object android.os.AsyncTask<Params, ...
- SpringBoot | 第十二章:RabbitMQ的集成和使用
前言 上节讲了缓存数据库redis的使用,在实际工作中,一般上在系统或者应用间通信或者进行异步通知(登录后发送短信或者邮件等)时,都会使用消息队列进行解决此业务场景的解耦问题.这章节讲解下消息队列Ra ...
- SpringBoot | 第五章:多环境配置
前言 写上一篇看英文资料,耗费了心力呀,这章,相对来说简单点.也比较熟悉,但是这很实用.不扯了,开始~ 多环境配置 maven的多环境配置 springboot多环境配置 总结 老生常谈 多环境配置 ...
- PHPGGC学习----理论
本文首发于先知:https://xz.aliyun.com/t/5450 PHPGGC 是一款能够自动生成主流框架的序列化测试payload的工具,类似 Java 中的 ysoserial, 当前支持 ...
- [luogu 3369]普通平衡树(fhq_treap)
题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询x数的排名(排名定义为比当前数小的数的个数+1.若有多 ...
- Spring Cloud 服务发现和消费
服务的发现和消费 有了服务中心和服务提供者,下面我们来实现一个服务的消费者: 服务消费者主要完成两个任务——服务的发现和服务的消费,服务发现的任务是由Eureka客户端完成,而服务消费的任务是由Rib ...
- unobtrusive验证,ajax局部加载后验证失效解决方法
页面加载后运行此代码 $(function() {$.validator.unobtrusive.parse($("form")); }); 原因: 页面加载后unobtrusiv ...
- LeetCode Find Peak Element 找临时最大值
Status: AcceptedRuntime: 9 ms 题意:给一个数组,用Vector容器装的,要求找到一个临时最高点,可以假设有num[-1]和num[n]两个元素,都是无穷小,那么当只有一个 ...
- 撸了个 django 数据迁移工具 django-supertube
撸了个 django 数据迁移工具 django-supertube 支持字段映射和动态字段转化. 欢迎 star,issue https://github.com/FingerLiu/django- ...
- World Wind Java开发之四——搭建本地WMS服务器(转)
在提供地理信息系统客户端时,NASA还为用户提供了开源的WMS Server 服务器应用:World Wind WMS Server.利用这个应用,我们可以架设自己的WMS服务并使用自己的数据(也支持 ...