zoukankan      html  css  js  c++  java
  • HDU 1242——Rescue(优先队列)

    题意:

    一个天使a被关在迷宫里,她的很多小伙伴r打算去救她。求小伙伴就到她须要的最小时间。在迷宫里有守卫。打败守卫须要一个单位时间。假设碰到守卫必须要杀死他

    思路:

    天使仅仅有一个,她的小伙伴有非常多,所以能够让天使找她的小伙伴,一旦找到小伙伴就renturn。时间小的优先级高。优先队列搞定


    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #define fx(i,xi,n) for(int i=xi;i<n;++i)
    #define ms(s,i) memset(s,i,sizeof (s))
    
    using namespace std;
    int dir[4][2]={0,1,0,-1,1,0,-1,0},vis[210][210],flag,ans,n,m;
    char map[210][210];
    bool check(int x,int y){if(x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]&&map[x][y]!='#')return true;return false;}
    struct pp{int x,y,s;friend bool operator<(const pp a,const pp b){return a.s>b.s;}};
    void bfs(int x,int y)
    {
        priority_queue<pp> q;
        pp a,b;
        a.x=x;a.y=y;a.s=0;ms(vis,0);vis[x][y]=1;
        q.push(a);
        while(!q.empty()){
            a=q.top();q.pop();
    //        cout<<a.x<<" "<<a.y<<map[a.x][a.y]<<endl;
            fx(i,0,4){
                b.x=a.x+dir[i][0];b.y=a.y+dir[i][1];
                if(check(b.x,b.y)){
                    vis[b.x][b.y]=1;b.s=a.s+1;
                    if(map[b.x][b.y]=='r'){flag=1;ans=b.s;return ;}
                    if(map[b.x][b.y]=='x') {b.s++;}
                    q.push(b);
                }
            }
        }
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF){
        ms(map,'#');
        fx(i,0,n) scanf("%s",map[i]);
        int sx,sy;
        fx(i,0,n)fx(j,0,m){if(map[i][j]=='a'){sx=i;sy=j;}}
        flag=ans=0;
        bfs(sx,sy);
        if(flag) cout<<ans<<endl;
        else cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
        }
        return 0;
    }




  • 相关阅读:
    Java加密AES算法及spring中应用
    IO流理解方式小结
    Spring CommonsMultipartResolver上传文件小结
    SpringMvc项目加载顺序及上下文小结
    Session学习小结
    MySql实现Oracle的row_number()over(partition by ... order by ...)
    Oracle存储过程小解
    mysql存储过程小解
    Java final关键字
    linux 同步 rsync的使用——远程服务器同步配置
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5119024.html
Copyright © 2011-2022 走看看