Hdu-3085-nightmareⅱ (Two-way BFS)

Source: Internet
Author: User

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 is the ghosts in the maze. They'll kill the people. Now little Erriyue wants to know if he could find his girl friend before the ghosts find them.
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 is evil, every second they would divide into several parts to occupy the grids within 2 steps to them until the Y occupy the whole maze. You can suppose in every second the ghosts divide firstly then the little Erriyue and he girl friend start to move, And if little Erriyue or his girl friend arrive in a grid with a ghost, they would 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 the 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:
'. ' Denotes an empty place and 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 would contain exactly one letter M, one letter G and one letters Z.

Outputoutput A single integer s on one line, denotes Erriyue and his girlfriend would meet in the minimum time s if they CA N meet successfully, or output-1 denotes they failed to meet.
Sample Input
6XXXXXXXZ.. Zxxxxxxxm. G......... 5 6XXXXXXXZZ.. Xxxxxxxm ..... G... 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 2nd month
Sourcehdu 2nd "Vegetable-birds Cup" programming Open Contest
Idea: The place where the ghost is directly into the wall, then the girl Walk 1 steps, the boy walk 3 steps, meet directly return time. (Note: The Ghost's clone can also continue to divide)
#include <stdio.h> #include <string.h>struct{int x,y,step;} Que1[1000000],que2[1000000],que[1000000],t;int n,m,gxa,gya,gxb,gyb,xa,ya,xb,yb,nxt[4][2]={{0,1},{1,0},{0,-1},{- 1,0}},top,bottom,gt;char mp[800][805];bool vis1[800][800],vis2[800][800],vis[800][800];void Divide ()//Ghost Fen,    Let the ghosts of the place become walls {int i;        while (Top<bottom) {t=que[top];        if (T.STEP&GT;=GT) return;        t.step++;            for (i=0;i<4;i++) {t.x+=nxt[i][0];            T.Y+=NXT[I][1];                if (t.x>=0 && t.x<n && t.y>=0 && t.y<m &&!vis[t.x][t.y]) {                Vis[t.x][t.y]=1;                mp[t.x][t.y]= ' x ';            que[bottom++]=t;            } T.x-=nxt[i][0];        T.Y-=NXT[I][1];    } top++;    }}int BFS () {int i,t1,t2,top1,top2,bottom1,bottom2;    memset (vis,0,sizeof Vis);    memset (vis1,0,sizeof vis1);    memset (vis2,0,sizeof vis2);    Top=0;    bottom=2;   QUE[0].X=GXA; Que[0].y=gya;    Que[0].step=0;    QUE[1].X=GXB;    Que[1].y=gyb;    Que[1].step=0;    gt=2;    T1=1;    t2=3;    top1=0;    Bottom1=1;    Que1[0].x=xa;    Que1[0].y=ya;    Que1[0].step=0;    Vis1[xa][ya]=1;    top2=0;    Bottom2=1;    QUE2[0].X=XB;    Que2[0].y=yb;    Que2[0].step=0;    Vis2[xb][yb]=1;        while (1) {divide ();//Ghost First fen bool flag=0;            while (TOP1&LT;BOTTOM1)//girl walk {T=QUE1[TOP1];            if (T.STEP&GT;=T1) break;            t.step++;                for (i=0;i<4;i++) {t.x+=nxt[i][0];                T.Y+=NXT[I][1]; if (t.x>=0 && t.x<n && t.y>=0 && t.y<m && mp[t.x-nxt[i][0]][t.y-nxt[i][1]]!                    = ' x ' && mp[t.x][t.y]!= ' x ' &&!vis1[t.x][t.y]) {flag=1;                    if (Vis2[t.x][t.y]) return t1;                    Vis1[t.x][t.y]=1;                que1[bottom1++]=t; } T.X-=NXT[I][0];            T.Y-=NXT[I][1];        } top1++;            } while (TOP2&LT;BOTTOM2)//boy walk {T=QUE2[TOP2];            if (t.step>=t2) break;            t.step++;                for (i=0;i<4;i++) {t.x+=nxt[i][0];                T.Y+=NXT[I][1]; if (t.x>=0 && t.x<n && t.y>=0 && t.y<m && mp[t.x-nxt[i][0]][t.y-nxt[i][1]]!                    = ' x ' && mp[t.x][t.y]!= ' x ' &&!vis2[t.x][t.y]) {flag=1;                    if (Vis1[t.x][t.y]) return t1;                    Vis2[t.x][t.y]=1;                que2[bottom2++]=t;                } T.x-=nxt[i][0];            T.Y-=NXT[I][1];        } top2++;        } if (!flag) return-1;        gt+=2;        t1++;    t2+=3;    }}int Main () {int t,i,j,cnt;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&m); for (i=0;i<n;i++) scanf ("%s", Mp[i]);        cnt=0;            for (i=0;i<n;i++) for (j=0;j<m;j++) {if (mp[i][j]== ' G ') xa=i,ya=j;            else if (mp[i][j]== ' M ') xb=i,yb=j;                else if (mp[i][j]== ' Z ') {mp[i][j]= ' X ';                if (!cnt) gxa=i,gya=j;                else gxb=i,gyb=j;            cnt++;    }} printf ("%d\n", BFS ()); }}

Hdu-3085-nightmareⅱ (Two-way BFS)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.