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
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
 
Author
二日月
 
 
 
 
双向BFS第一题,刚开始每次拓展节点WA了,其实应该是每次拓展一层。写的时候忘了优先拓展队列元素的少的那一边,不过没T,以后写的时候再加入吧。
这题最让我郁闷的是,读图的时候每次读入一个符号,即scanf(" %c",&MAP[i][j])这样,居然T了,调了半天,最后改成每次读入一行后过了,甚是不解啊
 #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)的更多相关文章

  1. HDU 3085 Nightmare Ⅱ 双向BFS

    题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...

  2. Nightmare Ⅱ HDU - 3085 (双向bfs)

    Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were tra ...

  3. HDU - 3085 Nightmare Ⅱ

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

  4. HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ)

    HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

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

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

  6. HDU 3085 Nightmare Ⅱ(双向BFS)

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

  7. HDU3085 Nightmare Ⅱ —— 双向BFS + 曼哈顿距离

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Other ...

  8. 2017多校第10场 HDU 6171 Admiral 双向BFS或者A*搜索

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6171 题意: 给你一个高度为6的塔形数组,你每次只能将0与他上下相邻的某个数交换,问最少交换多少次可以 ...

  9. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

随机推荐

  1. Spark SQL概念学习系列之Spark生态之Spark SQL(七)

    具体,见

  2. #c word转换PDF

    需要引用Microsoft.Office.Interop.Word,版本是07之上的. 这个版本会判断文件是否被占用. using Microsoft.Office.Interop.Word; usi ...

  3. 转载 SQL Server中索引管理之六大铁律

    转载原地址 http://jingyan.baidu.com/article/48a42057c03bd7a924250429.html 索引是以表列为基础的数据库对象.索引中保存着表中排序的索引列, ...

  4. [转]MyEclipse for Spring2014破解

    转至:http://blog.my-eclipse.cn/myeclipse-2014-crack.html 一.安装完成MyEclipse2014(适用于2013等版本)后,不要打开软件,下载破解附 ...

  5. AppServ 配置还是成功了

    安装就不说了,一键式安装,还不错. 首先,登陆密码吧,用户是root ,密码是空: 之前创建的databases都还在, 可以用phpmyadmin管理,不在输入复杂的命令了, 方便,继续鼓捣php.

  6. nginx将http重定向到https

    1.rewrite server { listen 80; server_name test.com; rewrite ^(.*)$ https://$host$1 permanent; } 2. n ...

  7. 应用XML作为数据库的快速开发框架

    背景 我经常应用C#开发一些小的桌面程序,这些桌面程序往往有以下几个特点: 程序比较小,开发周期很短. 程序的数据量不大,多数情况下不超过1万行记录. 对程序的性能要求不高. 程序并发很少或者基本没有 ...

  8. 【M7】千万不要重载&&,||和,操作符

    1.C++对于真假值表达式采用“骤死式”评估方法,比如&&,||. if( p!=NULL && strlen(p)>10)   如果p为NULL,后面的strl ...

  9. 如何给你的VS2010添加创建文件后的头注释

    修改VS自带的模板 1) 类文件 D:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplatesCache\ ...

  10. codeforces Gym 100500C D.Hall of Fame 排序

    Hall of Fame Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/attachmen ...