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.
InputThe 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.
OutputOutput 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
题意:一张n*m的地图,有一个男孩、一个女孩和两只鬼,每秒男孩走三步,女孩每秒一步,鬼每秒扩张2,也就是k秒后,所有与鬼的曼哈顿距离不大于2*k都会被鬼覆盖,求相遇最短时间。
思路:既然求最短时间,那么很容易想到bfs,对男孩bfs的同时对女孩bfs,当两者相遇的时候就是最短时间。
注:相当于鬼先走,然后人在走;另外注意模块化,我一开始写直接把男孩的三步用三重for去写,其实可以把男孩当成3次一步就好
#include<bits/stdc++.h> using namespace std; char maps[808][808]; int ways[4][2] = {0,1,1,0,-1,0,0,-1}; int vis[808][808]; int n,m,t; int ans; struct Node { int x,y; Node(int x=0,int y=0):x(x),y(y) {} } ghost[2],girl,boy; void init() { scanf("%d%d",&n,&m); int cnt = 0; memset(vis,0,sizeof(vis)); for(int i=1; i<=n; i++) { scanf("%s",maps[i]+1); for(int j=1; j<=m; j++) { if(maps[i][j] == 'M') boy = Node(i,j); else if(maps[i][j] == 'G') girl = Node(i,j); else if(maps[i][j] == 'Z') ghost[cnt++] = Node(i,j); } } } bool check(int x,int y,int k) { if(x < 1 || x > n || y < 1 || y > m) return 0; if(maps[x][y] == 'X') return 0; if(abs(x - ghost[0].x) + abs(y - ghost[0].y) <= 2*k) return 0; if(abs(x - ghost[1].x) + abs(y - ghost[1].y) <= 2*k) return 0; return 1; } queue<Node>que[2]; bool bfs(int s) { int t = que[s].size(); while(t--) { Node tmp = que[s].front(); que[s].pop(); if(!check(tmp.x,tmp.y,ans)) continue; for(int i=0; i<4; i++) { int xx = tmp.x + ways[i][0]; int yy = tmp.y + ways[i][1]; if(check(xx,yy,ans)) { if(vis[xx][yy] && (vis[xx][yy]&1) != ((s+2)&1)) return 1; else if(!vis[xx][yy])que[s].push(Node(xx,yy)),vis[xx][yy] = s+2; } } } return 0; } int cal() { memset(vis,0,sizeof(vis)); while(!que[0].empty())que[0].pop(); while(!que[1].empty())que[1].pop(); que[0].push(boy); que[1].push(girl); vis[boy.x][boy.y] = 2; vis[girl.x][girl.y] = 3; ans = 0; while(!que[0].empty() || !que[1].empty()) { ans++; if(bfs(0)) return ans; if(bfs(0)) return ans; if(bfs(0)) return ans; if(bfs(1)) return ans; } return -1; } int main() { scanf("%d",&t); while(t--) { init(); printf("%d ",cal()); } }