[hdu P3085] Nightmare Ⅱ

[hdu P3085] Nightmare Ⅱ

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2885 Accepted Submission(s): 806

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

 

练一下双向BFS。。

对于这题来说,有起点和终点,如果单单的BFS会有点吃力,而采用双向BFS,不仅效率高了很多,写起来也较方便和容易。

ghost这个因素可以放轻一点,因为它能穿越所有格子,所以判断在某一时刻前,他们能不能占领某块地可以直接用曼哈顿距离来判断。

剩下的就是M和G了。

由于M一秒走3步,G一秒走一步,所以,M要做3遍扩展。

双向BFS的框架类似于下:

 

1 while (!q[1].empty()&&!q[2].empty()) {
2     bool flag0=bfs(0,...),flag1=bfs(1,...);
3     if (flag0||flag1) return calc(step);
4     step...;
5 }
6 return ...;

 

对于这里来说,bfs里面需要注意一下,因为当前时刻要沿用上一时刻的状态,所以要用临时队列存一下。

code:

 1 #include<bits/stdc++.h>
 2 #define Ms(a,x) memset(a,x,sizeof a)
 3 using namespace std;
 4 const int N=805,fl[4][2]={{0,1},{-1,0},{1,0},{0,-1}};
 5 const char peo[]={'M','G'};
 6 struct pos {int x,y;}cur,nxt,gh[3];
 7 int n,m,sx,sy,tx,ty,steps,cg;
 8 char a[N][N]; queue <pos> Q[3];
 9 bool cannotreach(pos now) {
10     for (int i=1; i<=cg; i++)
11         if (abs(now.x-gh[i].x)+abs(now.y-gh[i].y)<=2*steps) return 1;
12     return 0;
13 }
14 bool bfs(int p,int c) {
15     Q[2]=Q[p];
16     for (int i=1; i<=c; i++) {
17         for (; !Q[2].empty(); ) {
18             cur=Q[2].front(),Q[2].pop(),Q[p].pop();
19             if (cannotreach(cur)) continue;
20             for (int i=0; i<4; i++) {
21                 nxt.x=cur.x+fl[i][0],nxt.y=cur.y+fl[i][1];
22                 if (nxt.x<1||nxt.x>n||nxt.y<1||nxt.y>m) continue;
23                 if (a[nxt.x][nxt.y]=='X'||cannotreach(nxt)) continue;
24                 if (a[nxt.x][nxt.y]==peo[1-p]) return 1;
25                 if (a[nxt.x][nxt.y]==peo[p]) continue;
26                 a[nxt.x][nxt.y]=peo[p],Q[p].push(nxt);
27             }
28         }
29         Q[2]=Q[p];
30     }
31     return 0;
32 }
33 int double_bfs() {
34     while (!Q[0].empty()) Q[0].pop();
35     while (!Q[1].empty()) Q[1].pop();
36     cur.x=sx,cur.y=sy,Q[0].push(cur);
37     cur.x=tx,cur.y=ty,Q[1].push(cur);
38     for (steps=1; !Q[0].empty()&&!Q[1].empty(); ) {
39         bool tag0=bfs(0,3),tag1=bfs(1,1);
40         if (tag0||tag1) return steps; else steps++;
41     }
42     return -1;
43 }
44 int main() {
45     int T; cin>>T;
46     for (; T; T--) {
47         scanf("%d%d",&n,&m),cg=0;
48         for (int i=1; i<=n; i++) {
49             scanf("%s",a[i]+1);
50             for (int j=1; j<=m; j++) {
51                 switch (a[i][j]) {
52                     case 'M':sx=i,sy=j;break;
53                     case 'G':tx=i,ty=j;break;
54                     case 'Z':gh[++cg]=(pos){i,j};break;
55                     default :break;
56                 }
57             }
58         }
59         printf("%d\n",double_bfs());
60     }
61     return 0;
62 }
View Code

 

转载于:https://www.cnblogs.com/whc200305/p/7688397.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值