標籤: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;}