HDU 1242——Rescue(優先隊列)

來源:互聯網
上載者:User

標籤:style   color   os   strong   io   for   ar   時間   

題意:

一個天使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;}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.