Nightmare Ⅱ
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 720 Accepted Submission(s): 136
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.
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.
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
Author
二日月
Source
注意的地方
1:鬼可以穿过墙。
2:每一步鬼要先走,如果发现此事G,M被覆盖了,说明,呵呵,他(她)走不了。
You can suppose that at every second the ghosts divide firstly.
不然第三组测试数据,不好解释。
3.关于M的三步,很显然,如果前一步走不了,就不能在从这一点继续走下去了。
就是要注意,如果走一步的时候不能走,那么就不能在这个点基础上又走一步。
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<cstdlib> 5 #include<queue> 6 using namespace std; 7 8 int n,m,step; 9 int zx1,zy1,zx2,zy2; 10 int mx,my,gx,gy; 11 int to[4][2]={{1,0},{0,1},{0,-1},{-1,0}}; 12 char a[810][810]; 13 bool hash[2][801][801],glag; 14 struct node 15 { 16 int x,y; 17 }; 18 queue<node>Q[2]; 19 20 int Min(int x,int y) 21 { 22 return x>y? y:x; 23 } 24 bool fun(node &t) 25 { 26 int one,two; 27 if(t.x>=1&&t.x<=n && t.y>=1&&t.y<=m && a[t.x][t.y]!='X') 28 { 29 one=abs(t.x-zx1)+abs(t.y-zy1); 30 two=abs(t.x-zx2)+abs(t.y-zy2); 31 one=(one+1)/2; 32 two=(two+1)/2; 33 one=Min(one,two); 34 if(one>step) return false; 35 } 36 return true; 37 } 38 void bfs(int x) 39 { 40 int i,size1; 41 node t,cur; 42 size1=Q[x].size(); 43 while(size1--) 44 { 45 cur=Q[x].front(); 46 Q[x].pop(); 47 if(fun(cur))continue; 48 for(i=0;i<4;i++) 49 { 50 t=cur; 51 t.x=t.x+to[i][0]; 52 t.y=t.y+to[i][1]; 53 if(fun(t))continue; 54 if(hash[x][t.x][t.y])continue; 55 hash[x][t.x][t.y]=true; 56 if(hash[x^1][t.x][t.y]) 57 { 58 glag=true; 59 return; 60 } 61 Q[x].push(t); 62 } 63 } 64 } 65 void dbfs() 66 { 67 node t; 68 t.x=mx; 69 t.y=my; 70 hash[0][t.x][t.y]=true; 71 Q[0].push(t);//boy 72 t.x=gx; 73 t.y=gy; 74 hash[1][t.x][t.y]=true; 75 Q[1].push(t);//girl 76 step=0; 77 glag=false; 78 while(true) 79 { 80 if(Q[0].size()==0 && Q[1].size()==0)break; 81 step++; 82 bfs(0); 83 bfs(0); 84 bfs(0); 85 bfs(1); 86 if(glag==true)break; 87 } 88 if(glag==true)printf("%d ",step); 89 else printf("-1 "); 90 } 91 void solve() 92 { 93 memset(hash,false,sizeof(hash)); 94 while(!Q[0].empty()){ 95 Q[0].pop(); 96 } 97 while(!Q[1].empty()){ 98 Q[1].pop(); 99 } 100 dbfs(); 101 } 102 int main() 103 { 104 int T; 105 bool flag; 106 int i,j; 107 scanf("%d",&T); 108 while(T--) 109 { 110 scanf("%d%d",&n,&m); 111 for(i=1;i<=n;i++) 112 scanf("%s",a[i]+1); 113 for(i=1,flag=false;i<=n;i++) 114 for(j=1;j<=m;j++) 115 { 116 if(a[i][j]=='Z' && !flag){ 117 zx1=i; 118 zy1=j; 119 flag=true; 120 } 121 else if(a[i][j]=='Z' && flag){ 122 zx2=i; 123 zy2=j; 124 } 125 if(a[i][j]=='M'){ 126 mx=i; 127 my=j; 128 } 129 if(a[i][j]=='G'){ 130 gx=i; 131 gy=j; 132 } 133 } 134 solve(); 135 } 136 return 0; 137 }