hdu 3085 Nightmare Ⅱ (双向bfs)

Nightmare Ⅱ

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


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 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
HDU 2nd “Vegetable-Birds Cup” Programming Open Contest
 


ps:出题人的意图和题意不符合,汗!猜题意纠结了好半天,最后是看别人的代码才把题意猜出来的 o(︶︿︶)o 这题真心不推荐。
要做的话,注意一下几点:
1.人不能站着不动。
2.一个人只要走到对方已经走过的点即算成功。 -_-|||  这真的和descrition不符合。

思路:
题意搞清楚后就还是很好做了。只需要把ghosts抽象出来就够了,判断一个点到他的距离即可判断这点是否能访问。然后就是M搜3步,G搜一步bfs了。

代码:
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define maxn 805
#define mod 1000000000
#define INF 0x3f3f3f3f
using namespace std;

int n,m,ans;
int sx1,sy1,sx2,sy2,step;
int xz[2],yz[2];
bool vis1[maxn][maxn],vis2[maxn][maxn];
char mp[maxn][maxn];
char s[maxn];
int dx[]= {-1,1,0,0};
int dy[]= {0,0,-1,1};
struct Node
{
    int x,y;
} cur,now,q1[1000000],q2[1000000];

bool isok(int tx,int ty)
{
    int t;
    if(tx<1||tx>n||ty<1||ty>m||mp[tx][ty]=='X') return false ;
    t=min(abs(xz[0]-tx)+abs(yz[0]-ty),abs(xz[1]-tx)+abs(yz[1]-ty));
    if(t<=2*step) return false ;
    return true ;
}
bool bfs()
{
    int i,j,k,u,nx,ny,tx,ty,flag;
    int head1=0,tail1=-1,head2=0,tail2=-1,tmp;
    memset(vis1,0,sizeof(vis1));
    memset(vis2,0,sizeof(vis2));
    cur.x=sx1;
    cur.y=sy1;
    vis1[sx1][sy1]=1;
    q1[++tail1]=cur;
    cur.x=sx2;
    cur.y=sy2;
    vis2[sx2][sy2]=1;
    q2[++tail2]=cur;
    step=1;
    while(1)
    {
        flag=0;
        for(k=1; k<=3; k++)
        {
            tmp=tail1;
            while(head1<=tmp)
            {
                flag=1;
                now=q1[head1];
                head1++;
                nx=now.x;
                ny=now.y;
                if(!isok(nx,ny)) continue ;
                for(i=0; i<4; i++)
                {
                    tx=nx+dx[i];
                    ty=ny+dy[i];
                    if(!isok(tx,ty)||vis1[tx][ty]) continue ;
                    if(vis2[tx][ty])
                    {
                        ans=step;
                        return true ;
                    }
                    vis1[tx][ty]=1;
                    cur.x=tx;
                    cur.y=ty;
                    q1[++tail1]=cur;
                }
            }
        }
        tmp=tail2;
        while(head2<=tmp)
        {
            flag=1;
            now=q2[head2];
            head2++;
            nx=now.x;
            ny=now.y;
            if(!isok(nx,ny)) continue ;
            for(i=0; i<4; i++)
            {
                tx=nx+dx[i];
                ty=ny+dy[i];
                if(!isok(tx,ty)||vis2[tx][ty]) continue ;
                if(vis1[tx][ty])
                {
                    ans=step;
                    return true ;
                }
                vis2[tx][ty]=1;
                cur.x=tx;
                cur.y=ty;
                q2[++tail2]=cur;
            }
        }
        step++;
        if(!flag) break ;
    }
    return false ;
}
int main()
{
    int i,j,t,cxx;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        cxx=-1;
        for(i=1; i<=n; i++)
        {
            scanf("%s",s);
            for(j=1; j<=m; j++)
            {
                mp[i][j]=s[j-1];
                if(mp[i][j]=='Z') xz[++cxx]=i,yz[cxx]=j;
                else if(mp[i][j]=='M') sx1=i,sy1=j;
                else if(mp[i][j]=='G') sx2=i,sy2=j;
            }
        }
        if(bfs()) printf("%d\n",ans);
        else printf("-1\n");
    }
    return 0;
}







你可能感兴趣的:(搜索)