#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
char map[201][201];//map数组,模拟监狱
int M,N,direction[4][2]={ {1,0},{-1,0}, {0,1},{0,-1}};
struct node//结构体中包含优先队列
{
int x;
int y;
int step;
friend bool operator<(node x, node y)//优先队列格式~
{
return x.step > y.step;
}
};
void bfs(node);
int main()
{
node start;
while(~scanf("%d%d",&N,&M))
{
for(int i=1;i<=N;i++)
{
getchar();//吸收多余回车符
for(int j=1;j<=M;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='a')
{
start.x=i;
start.y=j;
start.step=0;
}
}
}
bfs(start);
}
return 0;
}
void bfs(node x)
{
node a,b;
priority_queue <node> q;a//定义一个名为 q 的优先队列
q.push(x);//首元素进队~
while(!q.empty())
{
a=q.top();
q.pop();//与此同时清空这个元素以便为下个元素腾出位置
for(int i=0;i<4;i++)//一层一层的搜索是BFS广度的特点
{
b.x=a.x+direction[i][0];//代表‘上’‘下’方向
b.y=a.y+direction[i][1];//代表‘左’ ‘右’方向
b.step=a.step+1;
if(b.x<1||b.y<1||b.x>N||b.y>M||map[b.x][b.y]=='#')
continue ;//越界或者遇到墙的阻碍结束本次循环继续执行下一次
if(map[b.x][b.y]=='x')
b.step++;//此处需特别注意,KILL警卫要花费的额外时间
if(map[b.x][b.y]=='r')
{
printf("%d\n",b.step);
return;
}
map[b.x][b.y]='#';//用 # 来标记为走过
q.push(b);
}
}
printf("Poor ANGEL has to stay in the prison all his life.\n");
return ;
}
解题报告公主
最新推荐文章于 2020-09-23 11:00:46 发布