//使用了优先队列
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int x[]={-1,1,0,0},y[]={0,0,-1,1};
int visited[220][220],n,m,x2,y2,ok,min1;
char map[220][220];
typedef struct node1
{
int x,y,t;
}Q;
Q node;
priority_queue<node1> q; ; //声明结构体优先队列
bool operator<(Q q1,Q q2)//重载"<"运算符
{
return q1.t>q2.t;//声明为小的优先,注意与sort相反
}
int bfs()
{
int a,b,t1,x1,y1;
while(!q.empty())
{
node=q.top();
q.pop();
t1=node.t;
x1=node.x;y1=node.y;
for(int k=0;k<4;k++)
{
a=x1+x[k];b=y1+y[k];
if(a>=0&&a<n&&b>=0&&b<m&&map[a][b]!='#'&&!visited[a][b])
{
if(map[a][b]=='.')
{
visited[a][b]=1;
node.x=a;node.y=b;node.t=t1+1;
q.push(node);
}
else if(map[a][b]=='x')
{
visited[a][b]=1;
node.x=a;node.y=b;node.t=t1+2;
q.push(node);
}
else if(map[a][b]=='a')
return t1+1;
}
}
}
}
int main()
{
int i,j,x1,y1;
while(cin>>n>>m)
{
for(i=0;i<n;i++)
cin>>map[i];
min1=-1;
while(!q.empty()) q.pop(); //清空队列
memset(visited,0,sizeof(visited));
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(map[i][j]=='r') //将要搜索的所有的点都入队,然后依次搜索
{
map[i][j]='.';
node.x=i;
node.y=j;
node.t=0;
q.push(node);
visited[i][j]=1;
}
}
/* min1=bfs();
if(min1==-1)
cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
else
cout<<min1<<endl;*/
int ans=-1;
ans = bfs();
if( ans != -1 )
printf("%d/n",ans);
else
printf("Poor ANGEL has to stay in the prison all his life./n");
}
return 0;
}
zoj --1649
最新推荐文章于 2019-07-07 04:49:19 发布