zoukankan      html  css  js  c++  java
  • 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
    ";
            else
                cout<<"no
    ";
        }
        return 0;
    }
    anytime you feel the pain.hey,refrain.don't carry the world upon your shoulders
  • 相关阅读:
    Java基础01
    架构漫谈阅读笔记1
    机器学习-分类算法之k-近邻
    机器学习-模型选择
    机器学习-scikit-learn数据集
    机器学习-特征选择
    机器学习-数据的特征预处理
    实现模式阅读笔记二
    实现模式阅读笔记一
    《架构之美》阅读笔记七
  • 原文地址:https://www.cnblogs.com/gaoss/p/4975915.html
Copyright © 2011-2022 走看看