题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085
Nightmare Ⅱ
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2790 Accepted Submission(s): 781
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 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
Author
二日月
Source
HDU 2nd “Vegetable-Birds Cup” Programming Open Contest
题意:
1.Z一秒可以扩散到两步以内的范围,每一步都可以走四个方向, 可以穿墙。
2.M一秒可以走0123步, 每一步都可以是四个方向, 不可穿墙。
3.G一秒可以走01步, 每一步都可以是四个方向, 不可穿墙。
题解:
1.M和G是否可以相遇,典型的双向BFS问题。
2.由于ghost可以穿墙, 所以对于整个图来说,ghost畅通无阻, 因此可以利用曼哈顿距离来判断M和G是否会被抓到,避免了ghost加入队列的麻烦。
代码如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include