Nightmare2

传送门HDU3085

描述

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.
For example, given “ACGT”,”ATGC”,”CGTT” and “CAGT”, you can make a sequence in the following way. It is the shortest but may be not the only one.

输入

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 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.

样例

  • 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

  • Output
    1
    1
    -1

题解

  • 题意:G和M在迷宫里寻找对方,M每秒可以走3步,G每次走一步,且只能上下左右走。迷宫里有鬼,鬼每次分裂到上下左右两格位置,直到占满所有格子,鬼可以穿墙,求G和M在不遇到鬼的情况下的最短相遇时间。
  • 双向bfs,由于M每次走3步,所以M每次要处理3次。
  • 由于鬼可以穿墙,所以判断人和鬼是否相遇时根据曼哈顿距离来算就行。当时间time*2<曼哈顿距离时会和鬼相遇。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<bits/stdc++.h>
#define INIT(a,b) memset(a,b,sizeof(a))
#define LL long long
using namespace std;
const int inf=0x3f3f3f3f;
const int N=1e3+7;
const int mod=1e9+7;
char mp[N][N];
int vis[2][N][N];
int dir[4][2]={1,0,-1,0,0,1,0,-1};
int n,m,ans;
struct node{
int x,y;
}M,G,Z[2];
queue<node> que[2];
bool judge(int tab,node a){
for(int i=0;i<2;i++)
if(ans*2 >= abs(Z[i].x-a.x)+abs(Z[i].y-a.y) )
return false;
if(a.x<0||a.x>=n||a.y<0||a.y>=m)return false;
if(mp[a.x][a.y]=='X')return false;
return true;
}
int solve(int tab,int step){
node tem,q;
queue<node> qt;
qt=que[tab];
for(int i=0;i<step;i++){
while(!qt.empty()){
q=qt.front();qt.pop();que[tab].pop();
if(!judge(tab,q))continue;
for(int i=0;i<4;i++){
tem.x=q.x+dir[i][0],tem.y=q.y+dir[i][1];
if(!judge(tab,tem)||vis[tab][tem.x][tem.y])continue;
if(vis[tab^1][tem.x][tem.y]) return 1;
vis[tab][tem.x][tem.y]=1;
que[tab].push(tem);
}
}
qt=que[tab];
}
return 0;
}
int bfs(){
ans=0;
INIT(vis,0);
while(!que[0].empty())que[0].pop();
while(!que[1].empty())que[1].pop();
que[0].push(M);que[1].push(G);
vis[0][M.x][M.y]=vis[1][G.x][G.y]=1;
while(!que[0].empty()||!que[1].empty()){
ans++;
if(solve(0,3)||solve(1,1))
return ans;
}
return -1;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int z=0;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++){
//scanf("%s",mp[i]);
getchar();
for(int j=0;j<m;j++){
mp[i][j]=getchar();
if(mp[i][j]=='M') M=(node){i,j};
if(mp[i][j]=='G') G=(node){i,j};
if(mp[i][j]=='Z') Z[z++]=(node){i,j};
}
}
printf("%d\n",bfs());
}

return 0;
}
Donate comment here