hdu 1242

来源:互联网 发布:周杰伦听妈妈的话知乎 编辑:程序博客网 时间:2024/06/07 05:10

用到优先队列的BFS,遇到X加上2就可以了

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<queue>using namespace std;char map[205][205];int judge[205][205];int move[4][2]={{-1,0},{1,0},{0,-1},{0,1}};struct print{    int x,y;    int step;     friend bool operator < (const print &a,const print &b)  //时间小的先出队    {        return a.step>b.step;    }};int main(){    int n,m,sx,sy,fx,fy,ans;    while(scanf("%d%d",&n,&m)!=EOF){        memset(judge,0,sizeof(judge));        priority_queue<print> q;        for(int i=0;i<n;i++) scanf("%s",map[i]);        for(int i=0;i<n;i++){            for(int j=0;j<m;j++){                if(map[i][j]=='a'){                    sx=i;                    sy=j;                }            }        }        //while(q.size()) q.pop();        ans=-1;        print s;        s.x=sx;        s.y=sy;        s.step=0;        q.push(s);        judge[sx][sy]=1;        while(!q.empty()){            print now,next;            now=q.top();            //judge[now.x][now.y]=1;            q.pop();            if(map[now.x][now.y]=='r'){                ans=now.step;                break;            }            for(int i=0;i<4;i++){                next.x=now.x+move[i][0];                next.y=now.y+move[i][1];                if(judge[next.x][next.y]==0&&map[next.x][next.y]!='#'&&0<=next.x&&next.x<n&&0<=next.y&&next.y<m){                    judge[next.x][next.y]=1;                    if(map[next.x][next.y]=='x')                        next.step=now.step+2;                    else                        next.step=now.step+1;                    q.push(next);                }            }        }        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
原创粉丝点击