[hdu P3085] Nightmare Ⅱ

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2885 Accepted Submission(s): 806

Problem Description
Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were trapped in a big maze separately. More terribly, there are two ghosts in the maze. They will kill the people. Now little erriyue wants to know if he could find 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.

Input
The input starts with an integer T, means the number of test cases.
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.

Output
Output a single integer S in one line, denotes erriyue and his girlfriend will meet in the minimum time S if they can meet successfully, or output -1 denotes they failed to meet.

Sample Input
3
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

Sample Output
1
1
-1

练一下双向BFS。。

对于这题来说,有起点和终点,如果单单的BFS会有点吃力,而采用双向BFS,不仅效率高了很多,写起来也较方便和容易。

ghost这个因素可以放轻一点,因为它能穿越所有格子,所以判断在某一时刻前,他们能不能占领某块地可以直接用曼哈顿距离来判断。

剩下的就是M和G了。

由于M一秒走3步,G一秒走一步,所以,M要做3遍扩展。

双向BFS的框架类似于下:

 ].empty()&&!q[].empty()) {
     ,...),flag1=bfs(,...);
     if (flag0||flag1) return calc(step);
     step...;
 }
 return ...;

对于这里来说,bfs里面需要注意一下,因为当前时刻要沿用上一时刻的状态,所以要用临时队列存一下。

code:

 #include<bits/stdc++.h>
 #define Ms(a,x) memset(a,x,sizeof a)
 using namespace std;
 ,fl[][]={{,},{-,},{,},{,-}};
 const char peo[]={'M','G'};
 ];
 int n,m,sx,sy,tx,ty,steps,cg;
 ];
 bool cannotreach(pos now) {
     ; i<=cg; i++)
         *steps) ;
     ;
 }
 bool bfs(int p,int c) {
     Q[]=Q[p];
     ; i<=c; i++) {
         ].empty(); ) {
             cur=Q[].front(),Q[].pop(),Q[p].pop();
             if (cannotreach(cur)) continue;
             ; i<; i++) {
                 nxt.x=cur.x+fl[i][],nxt.y=cur.y+fl[i][];
                 ||nxt.x>n||nxt.y<||nxt.y>m) continue;
                 if (a[nxt.x][nxt.y]=='X'||cannotreach(nxt)) continue;
                 -p]) ;
                 if (a[nxt.x][nxt.y]==peo[p]) continue;
                 a[nxt.x][nxt.y]=peo[p],Q[p].push(nxt);
             }
         }
         Q[]=Q[p];
     }
     ;
 }
 int double_bfs() {
     ].empty()) Q[].pop();
     ].empty()) Q[].pop();
     cur.x=sx,cur.y=sy,Q[].push(cur);
     cur.x=tx,cur.y=ty,Q[].push(cur);
     ; !Q[].empty()&&!Q[].empty(); ) {
         ,),tag1=bfs(,);
         if (tag0||tag1) return steps; else steps++;
     }
     ;
 }
 int main() {
     int T; cin>>T;
     for (; T; T--) {
         scanf(;
         ; i<=n; i++) {
             scanf();
             ; j<=m; j++) {
                 switch (a[i][j]) {
                     case 'M':sx=i,sy=j;break;
                     case 'G':tx=i,ty=j;break;
                     case 'Z':gh[++cg]=(pos){i,j};break;
                     default :break;
                 }
             }
         }
         printf("%d\n",double_bfs());
     }
     ;
 }

[hdu P3085] 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 Ⅱ

    HDU - 3085 Nightmare Ⅱ 双向BFS,建立两个队列,让男孩女孩一起走 鬼的位置用曼哈顿距离判断一下,如果该位置与鬼的曼哈顿距离小于等于当前轮数的两倍,则已经被鬼覆盖 #includ ...

  3. hdu 1072 Nightmare (bfs+优先队列)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 Description Ignatius had a nightmare last night. H ...

  4. HDU 1072 Nightmare

    Description Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on ...

  5. HDU 3085 Nightmare II 双向bfs 难度:2

    http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...

  6. hdu - 1240 Nightmare && hdu - 1253 胜利大逃亡(bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1240 开始没仔细看题,看懂了发现就是一个裸的bfs,注意坐标是三维的,然后每次可以扩展出6个方向. 第一维代表在 ...

  7. HDU 3085 Nightmare Ⅱ (双向BFS)

    Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  8. HDU 1072 Nightmare (广搜)

    题目链接 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a ...

  9. HDU 3085 Nightmare Ⅱ(双向BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 题目大意:给你一张n*m地图上,上面有有 ‘. ’:路 ‘X':墙 ’Z':鬼,每秒移动2步,可 ...

随机推荐

  1. static final 内部类

    1 static关键字 1.1 static关键字概述类中的属性和方法都是通过创建本类对象调用的,当在调用对象的某个方法时,这个方法没有访问到对象的特有数据时,方法创建这个对象有些多余,可是不创建对象 ...

  2. Python加密保护解决方案

    防止代码反编译,高强度加密保护exe或pyc文件 产品简介 Python语言写的程序无需编译成二进制文件代码,可以直接从源代码运行程序.在计算机内部,Python解释器把源代码转换成字节码的中间形式, ...

  3. POI大数据Excel生成

    package com.hd.erpreport.controller; import java.io.File; import java.io.FileOutputStream; import ja ...

  4. node获取windows pc 机器的标示

    var exec = require('child_process').exec; if(process.platform != "win32"){ //window throw ...

  5. 56.关于vue项目的seo问题

    不可否定的是,vue现在火.但是在实际项目中,特别是像一下交互网站,我们不可避免会考虑到的是seo问题,这直接关系到我们网站的排名,很多人说用vue搭建的网站不能做优化,那我们真的要放弃vue,放弃前 ...

  6. CSRF与JSON

    之前遇到提交json的请求想要进行csrf攻击都是用的闭合表单的方法,很笨很麻烦, 这次看到了别人的操作记录一下. 这里用到了ajax异步请求(但是这里我有个疑问就是:这里用到了cors跨域,是不是必 ...

  7. Nginx技术研究系列7-Azure环境中Nginx高可用性和部署架构设计

    前几篇文章介绍了Nginx的应用.动态路由.配置.在实际生产环境部署时,我们需要同时考虑Nginx的高可用性和部署架构. Nginx自身不支持集群以保证自身的高可用性,商业版本的Nginx+推荐: T ...

  8. URL与视图

    path函数 path函数的定义为:path(route,view,name=None,kwargs=None).以下对这几个参数进行讲解. route 参数 url的匹配规则.这个参数中可以指定ur ...

  9. 使用查询分析器和SQLCMD分别登录远程的SQL2005的1434端口

    SQLCMD是操作SQLSERVER的一个命令行工具, 而查询分析器是它的图形工具     查询分析器(SQL2005下叫managerment studio),连接远程的SQLSERVER2005, ...

  10. Python RabbitMQ消息分发轮询

    1.收消息:一对多,默认依次轮询的发给每个消费端. 2.消息确认:默认RabbitMQ不会设置no_ack=Ture,意思是,当生产者给消费者发送发送消息时,消费者处理这个消息,处理完后会手动确认发送 ...