HDU 3085 Nightmare Ⅱ(噩夢 Ⅱ)

HDU 3085 Nightmare Ⅱ(噩夢 Ⅱ)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)app

 

Problem Description - 題目描述ide

  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.
昨晚,erriyue經歷了一場可怕的噩夢。他夢見本身和女友被困在一座大迷宮中。更糟的是,還有兩殺人惡鬼環伺其中。如今erriyue想知道本身是否能先於惡鬼找到女友。
你能夠認爲erriyue和他女友能夠移動4個方向。每回合,erriyue能夠移動3步且他女友能夠移動1步。惡鬼比較溜,能夠隨意分裂並不斷佔據2步內的格子,直到覆蓋迷宮。每回合鬼先動,人再動,若走到有鬼的格子,卒。
注意:新舊鬼皆能分裂。
CN

 

Input - 輸入測試

  The input starts with an integer T, means the number of test cases.spa

  Each test case starts with a line contains two integers n and m, means the size of the maze. (1<n, m<800)code

  The next n lines describe the maze. Each line contains m characters. The characters may be:orm

    ‘.’ denotes an empty place, all can walk on.xml

    ‘X’ denotes a wall, only people can’t walk on.htm

    ‘M’ denotes little erriyueblog

    ‘G’ denotes the girl friend.ip

    ‘Z’ denotes the ghosts.

  It is guaranteed that will contain exactly one letter M, one letter G and two letters Z. 

輸入開頭爲整數T,表示測試用例數。
每組測試用例頭一行有兩個整數n和m,表示迷宮大小。(1<n, m<800)
隨後n行迷宮。每行m個字符,表意以下:
‘.’ 空格,誰都能走。
‘X’ 牆,惟鬼能過。
‘M’ erriyue。
‘G’ 女友。
‘Z’ 惡鬼。
數據保證僅有一個M,一個G和兩個Z。
CN

 

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.

若是erriyue 和女友能夠在最短期S成功相遇,則輸出整數S在單獨一行;不然輸出-1
CN

 

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

 

題解

  兩層BFS(固然雙向也能夠)

  人能夠等人,人不能穿鬼,鬼比人快1s……

  而後把兩層結果比較一下就好了。

 

代碼 C++

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <queue>
 5 #define MX 805
 6 #define INF 0x7F7F7F7F
 7 struct Point {
 8     int y, x;
 9 }M, G, Z[2], now, nxt;
10 int data[MX][MX][2], drc[8] = { -1, 0, 1, 0, 0, -1, 0, 1 };
11 bool isCatch(int y, int x, int tim) {
12     return std::min(abs(y - Z[0].y) + abs(x - Z[0].x), abs(y - Z[1].y) + abs(x - Z[1].x)) <= tim * 2;
13 }
14 void BFS() {
15     int i = 0, j, tim, tmp;
16     std::queue<Point> q;
17     for (i = 0; i < 2; ++i) {
18         i ? q.push(G) : q.push(M);
19         while (!q.empty()) {
20             now = q.front(); q.pop();
21             tim = data[now.y][now.x][i] + 1;
22             tmp = i ? tim : (tim + 2) / 3;
23             if (isCatch(now.y, now.x, tmp)) continue;
24             for (j = 0; j < 8; j += 2) {
25                 nxt.y = now.y + drc[j]; nxt.x = now.x + drc[j + 1];
26                 if (isCatch(nxt.y, nxt.x, tmp) || data[nxt.y][nxt.x][i] <= tim) continue;
27                 data[nxt.y][nxt.x][i] = tim;
28                 q.push(nxt);
29             }
30         }
31     }
32 }
33 int main() {
34     char str[MX];
35     int t, i, j, n, m, iz, opt, tim;
36     scanf("%d", &t);
37     while (t--) {
38         scanf("%d%d ", &n, &m);
39         memset(data, -1, sizeof data); iz = 0;
40         for (i = 0; i < n; ++i) {
41             gets(str);
42             for (j = 0; j < m; ++j) {
43                 switch (str[j]) {
44                     case '.': data[i + 1][j + 1][0] = data[i + 1][j + 1][1] = INF; break;
45                     case 'Z': Z[iz].y = i + 1; Z[iz].x = j + 1; ++iz; break;
46                     case 'M': M.y = i + 1; M.x = j + 1; break;
47                     case 'G': G.y = i + 1; G.x = j + 1; break;
48                 }
49             }
50         }
51         data[M.y][M.x][1] = data[G.y][G.x][0] = INF;
52         data[M.y][M.x][0] = data[G.y][G.x][1] = 0;
53         BFS();
54         opt = INF;
55         for (i = 1; i <= n; ++i) {
56             for (j = 1; j <= m; ++j) {
57                 if ((data[i][j][0] | data[i][j][1]) == -1) continue;
58                 tim = std::max((data[i][j][0] + 2) / 3, data[i][j][1]);
59                 opt = std::min(opt, tim);
60             }
61         }
62         opt > MX ? puts("-1") : printf("%d\n", opt);
63     }
64     return 0;
65 }
相關文章
相關標籤/搜索