传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3085
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3738 Accepted Submission(s): 1073
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
‘.’ 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
题意:n*m的地图,有一个小e和他的女朋友还有两个鬼怪。x是墙(鬼能穿墙der),m是小e,g是女朋友,z是鬼怪。小e跑的贼快,每秒可以跑3个单位,女朋友每秒一个。每个鬼怪每秒可以将自己占据区域扩张两个单位距离,也就是说第k秒后所有和鬼怪的曼哈顿距离不超过2*k的位置都会被鬼怪占领。求小e和女朋友不见鬼的情况下最短碰头时间,见鬼出-1
思路:双向BFS,小e和女朋友各一个队列,每次扩张时如果新状态和鬼怪的曼哈顿距离如果<=秒数*2就不合法,当出现一个位置两个人都到达就ok
吐槽:忘记更新鬼怪的统计变量cnt,debug了好久quq
代码:
#include
#include
#include
#include
#include
#include
#include