HDU Problem - 3085 Nightmare Ⅱ(双向BFS)
題目鏈接
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
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..XSample Output
11-1AC
- 對鬼魂的處理:每次先計算鬼經過當前時間能否到達M和G的位置(鬼魂先移動),然后判斷當M和G移動之后能否到達(移動之后位置的合法性)
- 對于M和G:記錄他們所走的位置,因為雖然可能不是同時到達,但是如果他們都曾經到達一個地方(首先這個地方已經安全),那么其中一個人完全可以等著另一個人
- 用兩個BFS求最短距離
總結
以上是生活随笔為你收集整理的HDU Problem - 3085 Nightmare Ⅱ(双向BFS)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU Problem - 5113 B
- 下一篇: POJ 3061 -- Subseque