HDU 3085 Nightmare Ⅱ

来源:互联网 发布:美工如何提升自己 编辑:程序博客网 时间:2024/06/05 12:04

Nightmare Ⅱ

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


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
35 6XXXXXXXZ..ZXXXXXXXM.G.........5 6XXXXXXXZZ..XXXXXXXM.......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
11-1
 

Author
二日月
 

Source
HDU 2nd “Vegetable-Birds Cup” Programming Open Contest
 

Recommend
lcy

题意:两个幽灵先向周围两格分身(分身可以继续分身),男孩每次走三步,女孩每次走一步,
双向bfs,不必将两个幽灵压入队列,那样只会使问题复杂,只用考虑两个人到两个幽灵的距离是否大于当前步数的两倍,如果不是就表示被幽灵抓住,判断搜索结束的条件为是否经过对方经过的地方即可,因为可以看做对方没有走。

#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<cmath>#include<map>#include<set>#include<queue>using namespace std;#define for1(i,a) for(int (i)=1;(i)<=(a);(i)++)#define for0(i,a) for(int (i)=0;(i)<(a);(i)++)#define sf scanf#define pf printf#define mem(i,a) memset(i,a,sizeof i)const int dx[]={1,-1,0,0};const int dy[]={0,0,1,-1};int x,m,n,z[2][2],step;char g[801][801];bool vis[2][801][801];struct Node{    int x,y;    Node(int x,int y):x(x),y(y){}};bool check(Node p){    if(p.x<1||p.x>m||p.y<1||p.y>n||g[p.x][p.y]=='X')return 0;    for(int i=0;i<2;i++)        if(fabs(p.x-z[i][0])+fabs(p.y-z[i][1])<=(step<<1))//判断人到两个鬼的距离是否大于当前步数的两倍,不是就被抓            return 0;    return 1;}queue<Node>que[2],qt;int bfs(int p,int num)//num表示一次要走的步数,p表示是男孩还是女孩{    for(int i=0;i<num;i++)    {        qt=que[p];        while(!qt.empty())        {            Node tmp=qt.front();            qt.pop();            que[p].pop();            if(!check(tmp))continue;//鬼先走,这里的判断不能带上这个点是否走过            for(int j=0;j<4;j++)            {                Node po=tmp;                po.x+=dx[j],po.y+=dy[j];                if(!check(po)||vis[p][po.x][po.y])continue;                if(vis[p^1][po.x][po.y])return 1;//判断当前男孩或女孩是否走过对方经过的地方,如果是返回1                vis[p][po.x][po.y]=1;                que[p].push(po);            }        }    }    return 0;}void solve(){    while(!que[1].empty()&&!que[0].empty())    {        ++step;        int a=bfs(1,3);        int b=bfs(0,1);        if(a||b)        {            pf("%d\n",step);            return;        }    }    pf("-1\n");}int main(){    sf("%d",&x);    while(x--)    {        while(!que[0].empty())que[0].pop();        while(!que[1].empty())que[1].pop();        while(!qt.empty())qt.pop();        mem(vis[0],0);        mem(vis[1],0);        step=0;        sf("%d%d",&m,&n);        int p=0;        for(int i=1;i<=m;i++)        {            sf("%s",g[i]+1);            for(int j=1;j<=n;j++)            {                if(g[i][j]=='G')                    vis[0][i][j]=1,que[0].push(Node(i,j));                else if(g[i][j]=='M')                    vis[1][i][j]=1,que[1].push(Node(i,j));                else if(g[i][j]=='Z')                    z[p][0]=i,z[p++][1]=j;            }        }        solve();    }    return 0;}