【优先队列】HDU Rescue

来源:互联网 发布:中国网络什么时候墙的 编辑:程序博客网 时间:2024/05/01 00:02

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

 

利用优先队列处理守卫的时间差

 

#include<cstdio>#include<iostream>#include<queue>#include<cstring>#include<algorithm>using namespace std; struct node{    friend bool operator< (node n1, node n2)    {        return n1.step > n2.step;    }    int x,y;    int step;};priority_queue<node> q;int n,m;char map[220][220];int vis[220][220];int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};bool jud(int x,int y){    if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]!='#'&&!vis[x][y])            return true;    return false;}int bfs(node start){    while(!q.empty())q.pop();    node now,next;    q.push(start);    int i;    while(!q.empty()){        now=q.top();        q.pop();        vis[now.x][now.y]=1;        for(i=0;i<4;++i){            next.x=now.x+dir[i][0];            next.y=now.y+dir[i][1];            if(!jud(next.x,next.y))continue;             if(map[next.x][next.y]=='x')                next.step=now.step+2;            else                next.step=now.step+1;            if(map[next.x][next.y]=='a')                return next.step;            q.push(next);        }    }    return -1;}        int main(){    int i,j;    int ans;    node start;    while(scanf("%d%d\n",&n,&m)!=EOF){        ans=-1;        memset(vis,0,sizeof(vis));        for(i=0;i<n;++i){            for(j=0;j<m;++j){                scanf("%c",&map[i][j]);            }            getchar();        }        for(i=0;i<n;++i){            for(j=0;j<m;++j){                if(map[i][j]=='r'){                    start.x=i,start.y=j;                    start.step=0;                    ans=max(ans,bfs(start));                }            }        }        if(ans==-1){            printf("Poor ANGEL has to stay in the prison all his life.\n");        }else{            printf("%d\n",ans);        }    }    return 0;}


 

 

0 0
原创粉丝点击