我是靠谱客的博主 唠叨帽子,最近开发中收集的这篇文章主要介绍Rescue(BFS)Rescue ,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Rescue

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 9 Accepted Submission(s) : 4
Problem Description
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)

Input
First line contains two integers stand for N and M.

Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.

Process to the end of the file.

Output
For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."

Sample Input
7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........

Sample Output
13

Author
CHEN, Xue
题目大意:输入N行M列的字符串
          a代表所要查找的目标,r为起点,每移动一步则耗时1,遇到x耗时2,#为不可移动点
          问找到a时所用最少时间
  

题解:bfs即可,可能有多个’r’,而’a’只有一个,从’a’开始搜,找到的第一个’r’即为所求

需要注意的是这题宽搜时存在障碍物,遇到’x’点是,时间+2,如果用普通的队列就并不能保证每次出队的是时间最小的元素,所以要用优先队列

优先队列(priority_queue)的基本操作:

empty(); 队列为空返回1

pop(); 删除队首值

push(); 将元素归入

top(); 返回队列中优先级最高的元素

size(); 返回队列中元素的个数

具体见:http://blog.csdn.net/flqbestboy/article/details/7830782


#include<cstdio>
#include<queue>
using namespace std;
char map[210][210];//路线输入
bool mark[210][210];//标记此节点是否走过
int move[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//上下左右移动
int m,n,i,j,res;
struct node
{
int x,y,t;
friend bool operator<(const node &n1,const node &n2)
{
return n1.t>n2.t;
}//operator重载排序使,排序从大到小,队首值为最小值
}a,p,q;
int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
for(i=0;i<m;i++)
scanf("%s",map[i]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(map[i][j]=='a')
{
a.x=i,a.y=j,a.t=0;
break;
}
res=-1,memset(mark,true,sizeof(mark)),mark[a.x][a.y]=false;//数据初始化
priority_queue<node>Q;
Q.push(a);
while(!Q.empty())
{
q=Q.top(),Q.pop();
if(map[q.x][q.y]=='r')
{
res=q.t;
break;
}
for(i=0;i<4;i++)
{
p.x=q.x+move[i][0],p.y=q.y+move[i][1];
if(p.x>=0&&p.x<m&&p.y>=0&&p.y<n&&map[p.x][p.y]!='#'&&mark[p.x][p.y])
{
mark[p.x][p.y]=false;
if(map[p.x][p.y]=='x')
p.t=q.t+2;
else
p.t=q.t+1;
Q.push(p);
}
}
}
if(res!=-1)//这一步返回时没考虑0,调试了半天....
printf("%dn",res);
else
printf("Poor ANGEL has to stay in the prison all his life.n");
}
return 0;
}


最后

以上就是唠叨帽子为你收集整理的Rescue(BFS)Rescue 的全部内容,希望文章能够帮你解决Rescue(BFS)Rescue 所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部