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
  • 相关阅读:
    【Python第九篇】异步IO数据库队列缓存
    【Python第八篇】线程、进程及协程
    【Python第七篇】Socket网络编程
    实验五全部代码,ajax请求
    添加员工
    联级选择
    查询,利用jquery选择器
    列表、表格单选框
    注册
    聊天框
  • 原文地址:https://www.cnblogs.com/gaoss/p/4975915.html
Copyright © 2011-2022 走看看