hdu 逃離迷宮 1728

/*
以源點爲爲一個隊列,搜遍隊列所有元素的四個方向,存到另一個隊列,在遍歷另一個隊列
這樣我們就不用考慮,是否拐過彎了


*/
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
char Map[105][105];
int Map1[105][105],bu[4][2]= {0,1,0,-1,1,0,-1,0},n,m,T;
struct node
{
    int x,y,z;
    node()
    {
        z=-1;
    }
} S,D;
int bfs()
{
    queue<node> v,v1;
    while(!v.empty())
        v.pop();
    while(!v1.empty())
        v1.pop();
    int i;
    node l,r;
    v.push(S);
    Map1[S.x][S.y]=1;
    while(!v.empty())
    {
        while(!v.empty())
        {
            l=v.front();
            if(l.x==D.x&&l.y==D.y&&l.z<=T)
                return 1;
            v.pop();
            if(l.z>T)
                continue;
            //printf("+++%d %d %d\n",l.x,l.y,l.z);
            for(i=l.x+1; i<=n; i++)
            {
                if(Map[i][l.y]=='*')
                    break;
                if(Map1[i][l.y]==0)
                {
                    //puts("++++");
                    Map1[i][l.y]=1;
                    r.x=i;
                    r.y=l.y;
                    r.z=l.z+1;
                    v1.push(r);
                    if(r.x==D.x&&r.y==D.y&&r.z<=T)
                    {
                        return 1;
                    }
                }
            }
            for(i=l.x-1; i>0; i--)
            {
                if(Map[i][l.y]=='*')
                    break;
                if(Map1[i][l.y]==0)
                {
                    Map1[i][l.y]=1;
                    r.x=i;
                    r.y=l.y;
                    r.z=l.z+1;
                    v1.push(r);
                    if(r.x==D.x&&r.y==D.y&&r.z<=T)
                        return 1;
                }
            }
            for(i=l.y+1; i<=m; i++)
            {
                if(Map[l.x][i]=='*')
                    break;
                if(Map1[l.x][i]==0)
                {
                    Map1[l.x][i]=1;
                    r.x=l.x;
                    r.y=i;
                    r.z=l.z+1;
                    v1.push(r);
                    if(r.x==D.x&&r.y==D.y&&r.z<=T)
                        return 1;
                }
            }
            for(i=l.y-1; i>0; i--)
            {
                if(Map[l.x][i]=='*')
                    break;
                if(Map1[l.x][i]==0)
                {
                    Map1[l.x][i]=1;
                    r.x=l.x;
                    r.y=i;
                    r.z=l.z+1;
                    v1.push(r);
                    if(r.x==D.x&&r.y==D.y&&r.z<=T)
                        return 1;
                }
            }
        }
        while(!v1.empty())
        {
            v.push(v1.front());
            v1.pop();
        }
    }
    return 0;
}
int main()
{
    int t,i,j;
    scanf("%d",&t);
    while(t--)
    {
        memset(Map1,0,sizeof(Map1));
        scanf("%d%d",&n,&m);
        for(i=1; i<=n; i++)
            scanf("%s",Map[i]+1);
        scanf("%d%d%d%d%d",&T,&S.y,&S.x,&D.y,&D.x);
        int aa=bfs();
        if(aa)
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
}
/*
16 2
**
.*
..
..
..
.*
..
..
..
.*
.*
..
..
.*
..
..

2 2 13 2 13

//這樣的數據也應該輸出yes

*/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章