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. POJ 1236 Network of Schools (有向图的强连通分量)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9073   Accepted: 359 ...

  2. [iOS UI进阶 - 2.4] 彩票Demo v1.4 转盘动画

    A.需求 幸运广场界面中有一个幸运转盘,平时能够自动缓缓转动 能够选择星座 点击“开始选号”开速旋转转盘,旋转一定周数 转盘转动速度节奏:开始-慢-块-慢-结束 设置其余的背景和按钮   code s ...

  3. Codeforces Round #201 (Div. 2) - C. Alice and Bob

    题目链接:http://codeforces.com/contest/347/problem/C 题意是给你一个数n,然后n个数,这些数互不相同.每次可以取两个数x和y,然后可以得到|x - y|这个 ...

  4. 计算两个日期相隔的天数(jodd)

    public static void main(String[] args) throws ParseException { System.out.println(TimeUtil.dayOfYear ...

  5. 启动报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    如果你是maven项目,tomcat在发布项目的时候没有同时发布maven依赖所添加的jar包,你需要设置一下eclipse:项目 —> 属性 -> Deployment Assembly ...

  6. libevent的使用方法--回显服务器的简单实例

    #include <event.h> #include <sys/types.h> #include <sys/socket.h> #include <net ...

  7. Castle IOC容器快速入门

    主要内容 1.为什么要IOC 2.什么是Castle IOC容器 3.快速入门示例 4.几个重要的概念 一,为什么要IOC IOC(控制反转或者叫依赖注入)Martin Fowler大师在他的文章中已 ...

  8. Js面向对象和数据类型内存分配(转)

    一 Js基本数据类型以及内存情况 1 Undefined Undefined类型只有一个值undefined,在使用了声明但未初始化的变量的时候,这个变量值就是undefined 1 var hi; ...

  9. JS Math 类库介绍

    下面介绍下随机生成数的常用几个API JS 随机数生成 : 在JavaScript , 提供了生成随机数的API, Math.random() 1.Math.random() : 随机生成小数 . 生 ...

  10. Python3爬取百度百科(配合PHP)

    用PHP写了一个网页,可以获取百度百科词条.源代码已分享至github:https://github.com/1049451037/xiaobaike/tree/master 那么通过Python来爬 ...