zoukankan      html  css  js  c++  java
  • HDU 1242 (BFS搜索+优先队列)

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

    题目大意:多个起点到一个终点,普通点耗时1,特殊点耗时2,求到达终点的最少耗时。

    解题思路

    如果没有特殊点,就是普通BFS。

    由于特殊点的介入,所以BFS树的同一深度,各个点的值可能不同。所以使用优先队列,先取出值小的搜。

    搜到的第一个符合条件的结果肯定是最小的,break。

    注意有多个起点,所以先记录下所有起点,依次BFS找最小。

     

    #include "cstdio"
    #include "string"
    #include "cstring"
    #include "iostream"
    #include "vector"
    #include "queue"
    using namespace std;
    #define inf 1<<28
    struct status
    {
        int x,y,dep;
        status(int x,int y,int dep):x(x),y(y),dep(dep) {}
        bool operator < (const status &a) const
        {
            return dep>a.dep;
        }
    };
    int n,m,map[205][205],vis[205][205],dir[4][2]={-1,0,1,0,0,-1,0,1},ans;
    void bfs(int x,int y)
    {
        priority_queue<status> Q;
        Q.push(status(x,y,0));
        vis[x][y]=true;
        bool flag=false;
        while(!Q.empty())
        {
            if(flag) break;
            status t=Q.top();Q.pop();
            for(int s=0;s<4;s++)
            {
                int X=t.x+dir[s][0],Y=t.y+dir[s][1];
                if(vis[X][Y]||X<0||X>n||Y<0||Y>n||!map[X][Y]) continue;
                vis[X][Y]=true;
                if(map[X][Y]==2) {flag=true;ans=min(ans,t.dep+1);};
                if(map[X][Y]==3) Q.push(status(X,Y,t.dep+2));
                else Q.push(status(X,Y,t.dep+1));
            }
        }
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        ios::sync_with_stdio(false);
        string tt;
        while(cin>>n>>m&&n)
        {
            vector<status> f;
            ans=inf;
            for(int i=1;i<=n;i++)
            {
                cin>>tt;
                for(int j=0;j<tt.size();j++)
                {
                    if(tt[j]=='#') map[i][j+1]=0;
                    if(tt[j]=='.') map[i][j+1]=1;
                    if(tt[j]=='a') map[i][j+1]=2;
                    if(tt[j]=='x') map[i][j+1]=3;
                    if(tt[j]=='r') {map[i][j+1]=4;f.push_back(status(i,j+1,0));}
                }
            }
            for(int i=0;i<f.size();i++)
            {
                memset(vis,0,sizeof(vis));
                bfs(f[i].x,f[i].y);
            }
            if(ans!=inf) cout<<ans<<endl;
            else cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
        }
    }

      

    11869491 2014-10-14 14:50:56 Accepted 1242 31MS 612K 1861 B C++ Physcal
  • 相关阅读:
    木马控制技术(二) -- 端口复用
    OSQL.EXE 命令行下脱裤mssql
    移动App中常见的Web漏洞
    phpcms前台任意代码执行漏洞(php<5.3)
    LeetCode OJ--Copy List with Random Pointer **
    LeetCode OJ-- Container With Most Water
    LeetCode OJ--Combinations *
    LeetCode OJ-- Combination Sum II **
    LeetCode OJ--Combination Sum **
    LeetCode OJ-- Candy **
  • 原文地址:https://www.cnblogs.com/neopenx/p/4023458.html
Copyright © 2011-2022 走看看