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. Tomcat线程池,更符合大家想象的可扩展线程池

    因由 说起线程池,大家可能受连接池的印象影响,天然的认为,它应该是一开始有core条线程,忙不过来了就扩展到max条线程,闲的时候又回落到core条线程,如果还有更高的高峰,就放进一个缓冲队列里缓冲一 ...

  2. JodaTime用法简介

    JodaTime用法简介 Java的Date和Calendar用起来简直就是灾难,跟C#的DateTime差距太明显了,幸好有JodaTime 本文简单罗列JodaTime的用法 package co ...

  3. [C语言 - 3] 字符串

    字符数组 char * 看做一个特殊的字符数组, 在字符串结束为止添加'\0'结束符 (ASCII码0), 没有\0结尾的是普通的字符数组. 使用双引号定义的字符串自动在尾部加上\0 puts(s)函 ...

  4. Chrome的Postman的使用

    Chrome提供了一个很好的Web App 名为 Postman 使用这个web app,你可以输入一个url,然后可以很清楚的看到返回的各种结果 直接在Google中输入Postman, 找到它   ...

  5. HDU 4052 Adding New Machine (线段树+离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4052 初始给你w*h的矩阵,给你n个矩形(互不相交),按这些矩形尺寸把初始的矩形扣掉,形成一个新的'矩 ...

  6. POJ 1502 MPI Maelstrom (Dijkstra)

    题目链接:http://poj.org/problem?id=1502 题意是给你n个点,然后是以下三角的形式输入i j以及权值,x就不算 #include <iostream> #inc ...

  7. HDU 2874 Connections between cities (LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 题意是给你n个点,m条边(无向),q个询问.接下来m行,每行两个点一个边权,而且这个图不能有环路 ...

  8. Winform开发框架之通用自动更新模块(转)

    在网络化的环境中,特别是基于互联网发布的Winform程序,程序的自动更新功能是比较重要的操作,这样可以避免挨个给使用者打电话.发信息通知或者发送软件等,要求其对应用程序进行升级.实现程序的自动更新, ...

  9. MongoDB 快速入门--中级

    索引 ensureIndex 用来创建索引,需要注意的就是一个集合最多也就64个索引 如果没加所有就是表扫表,速度很慢, 当然如果索引的键有多个,就必须考虑顺序 拓展索引 同样的也可以为内嵌文档 建立 ...

  10. python求3的倍数与和

    suqares=[] i=1 sum=0 while i<=100: i+=1 if i*3: sum=sum+i # print(i) suqares.append(i*3) # print( ...