HDU 3085 Nightmare Ⅱ【BFS +曼哈頓距離+綜合性較強】

來源:互聯網
上載者:User

標籤:

Nightmare ⅡTime Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)Total Submission(s) : 16   Accepted Submission(s) : 6Font: Times New Roman | Verdana | GeorgiaFont Size: ← →Problem DescriptionLast 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.
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
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


#include<stdio.h>#include<queue>#include<math.h>#include<string.h>#include<stdlib.h>using namespace std;char map[810][810];int step[4][2]= {1,0,-1,0,0,1,0,-1};struct node{    int x,y;} M,G,Z[2];int num_of_step,n,m;queue<node> q[3];bool ghost(node a){    for(int i=0; i<2; i++)    {        if(abs(a.x-Z[i].x)+abs(a.y-Z[i].y) <= 2*num_of_step)//            return false;//被抓    }    return true;}bool check(node b){    if(b.x>=0&&b.y>=0&&b.x<n&&b.y<m&&map[b.x][b.y]!='X')        return true;    return false;}bool BFS(int people,int time,int start,int endd){    node cur,next;    q[2]=q[people];    for(int i=0; i<time; i++)    {        while(!q[2].empty())        {            cur=q[2].front();            q[2].pop();            q[people].pop();            if(ghost(cur))  //如果被抓            {                for(int i=0; i<4; i++)                {                    next=cur;                    next.x+=step[i][0];                    next.y+=step[i][1];                    if(ghost(next)&&check(next)&&map[next.x][next.y]!=start)                    {                        if(map[next.x][next.y]==endd)                            return true;                        map[next.x][next.y]=start;                        q[people].push(next);                    }                }            }        }        q[2]=q[people];    }    return false;}int solve (){    for(int i=0; i<3; i++) ////clear    {        while(!q[i].empty())            q[i].pop();    }    num_of_step=0;    q[0].push(M);    q[1].push(G);    while(!q[0].empty()&&!q[1].empty())////    {        num_of_step++;        if(BFS(0,3,'M','G')||BFS(1,1,'G','M'))////            return num_of_step;    }    return -1;}int main (void){    int N;    scanf("%d",&N);    while(N--)    {        int cnt=0;        scanf("%d%d",&n,&m);        //getchar();        for(int i=0; i<n; i++)        {            scanf("%s",map[i]);            for(int ii=0; ii<m; ii++)            {                if(map[i][ii]=='M')      M.x=i,M.y=ii;                else if(map[i][ii]=='G') G.x=i,G.y=ii;                else if(map[i][ii]=='Z') Z[cnt].x=i,Z[cnt].y=ii,++cnt;            }        }        //getchar();        printf("%d\n",solve());    }    return 0;}


..X.........M.X...X.X..........X..X.X.X..........X..XX....X.X....G...X...ZX.X......Z..X..X
Sample Output
11-1

HDU 3085 Nightmare Ⅱ【BFS +曼哈頓距離+綜合性較強】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.