hdu 1728 逃离迷宫

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1728

题目分类:bfs

代码

#include<bits/stdc++.h>

using namespace std;

char maze[105][105];
int Sx, Sy, Ex, Ey, k;
bool vis[105][105];
bool ok;
int n, m;

int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};

struct ST
{
    int ii;
    int jj;
    int wan;
};

bool Judge(ST a)
{
    if(a.ii<1||a.ii>n||a.jj<1||a.jj>m||maze[a.ii][a.jj]=='*')
        return 1;
    return 0;
}

void BFS()
{
    ST now;
    now.ii = Sx;
    now.jj = Sy;
    now.wan = -1;
    queue<ST > que;
    while(!que.empty())
        que.pop();
    que.push(now);

    while(!que.empty())
    {
        now = que.front();
        if(now.ii==Ex&&now.jj==Ey)
        {
            if(now.wan<=k)
                ok = 1;
            return ;
        }
        que.pop();
        for(int i=0;i<4;i++)
        {
            ST Next;
            Next.ii = now.ii + dx[i];
            Next.jj = now.jj + dy[i];
            if(Judge(Next)||now.wan>=k)
                continue;
            if(vis[Next.ii][Next.jj]&&Next.wan < now.wan + 1)
                continue;
            Next.wan = now.wan + 1;
            while(Next.ii>=1&&Next.ii<=n&&Next.jj>=1&&Next.jj<=m&&maze[Next.ii][Next.jj]=='.')
            {
                if(!vis[Next.ii][Next.jj])
                {
                    Next.wan = now.wan + 1;
                    if(Next.wan==k)
                    {
                        if(Next.ii!=Ex&&Next.jj!=Ey)
                            break;
                    }
                    vis[Next.ii][Next.jj] = 1;
                    que.push(Next);
                }
                Next.ii += dx[i];
                Next.jj += dy[i];
            }
        }
    }

    return ;
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                cin>>maze[i][j];
        cin>>k>>Sy>>Sx>>Ey>>Ex;
        memset(vis, 0, sizeof(vis));
        ok = 0;
        BFS();
        if(ok)
            cout<<"yes\n";
        else
            cout<<"no\n";
    }
    return 0;
}

 

你可能感兴趣的:(hdu 1728 逃离迷宫)