解题报告公主

#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 ;
 	
 }
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值