直接把Angle的位置作为起点,广度优先搜索即可,这题不是步数最少,而是time最少,就把以time作为衡量标准,加入优先队列,队首就是当前time最少的。遇到Angle的朋友就退出。只需15ms

AC代码:

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=202;
int d[maxn][maxn];
int n,m;
char G[maxn][maxn];
struct node{
	int x,y;
	node(){
	}
	node(int x,int y):x(x),y(y){
	}
	bool operator <(const node &p)const{
		return d[x][y]>d[p.x][p.y];
	}
}s;
const int dx[]={-1,1,0,0};
const int dy[]={0,0,-1,1};
int bfs(){
	memset(d,-1,sizeof(d));
	priority_queue<node>q;
	d[s.x][s.y]=0;
	q.push(s);
	while(!q.empty()){
		node p=q.top();
		q.pop();
		int x=p.x,y=p.y;
		if(G[x][y]=='r') return d[x][y];
		for(int i=0;i<4;++i){
			int px=x+dx[i],py=y+dy[i];
			if(px<0||py<0||px>=n||py>=m||d[px][py]!=-1||G[px][py]=='#') continue;
			if(G[px][py]=='x') d[px][py]=d[x][y]+2;
			else d[px][py]=d[x][y]+1;
			q.push(node(px,py));
		}
	}
	return -1;
}
void print(){
	for(int i=0;i<n;++i){
		for(int j=0;j<m;++j)
		printf("%02d ",d[i][j]);
		printf("\n");
	}

}
int main(){
	while(scanf("%d%d",&n,&m)!=EOF){
		for(int i=0;i<n;++i)
			scanf("%s",G[i]);
		for(int i=0;i<n;++i)
		for(int j=0;j<m;++j){
			if(G[i][j]=='a') s=node(i,j);
			else if(G[i][j]!='r'&&G[i][j]!='a'&&G[i][j]!='.'&&G[i][j]!='#') G[i][j]='x';
		}
		int ans=bfs();
		//print();
		if(ans==-1) printf("Poor ANGEL has to stay in the prison all his life.\n");
		else printf("%d\n",ans);
	}
}

如有不当之处欢迎指出!

hdu1242 Rescue bfs+优先队列的更多相关文章

  1. HDU1242 Rescue(BFS+优先队列)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  2. hdu1242 Rescue(BFS +优先队列 or BFS )

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 题意:     Angel被传说中神秘的邪恶的Moligpy人抓住了!他被关在一个迷宫中.迷宫的长.宽不超 ...

  3. Rescue HDU1242 (BFS+优先队列) 标签: 搜索 2016-05-04 22:21 69人阅读 评论(0)

    Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is describe ...

  4. poj1649 Rescue(BFS+优先队列)

    Rescue Time Limit: 2 Seconds      Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...

  5. HDU 1242 Rescue(BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...

  6. Rescue BFS+优先队列 杭电1242

    思路 : 优先队列 每次都取最小的时间,遇到了终点直接就输出 #include<iostream> #include<queue> #include<cstring> ...

  7. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

  8. POJ 1724 ROADS(BFS+优先队列)

    题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...

  9. hdu 1242 找到朋友最短的时间 (BFS+优先队列)

    找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...

随机推荐

  1. js 抛物线 笔记备份

    var funParabola = function(element, target, options) { /* * 网页模拟现实需要一个比例尺 * 如果按照1像素就是1米来算,显然不合适,因为页面 ...

  2. 使用SoapUI调用Vsphere Web Service

    项目中经常需要调用Webservice进行验证测试,下面就介绍下如何使用测试工具SoapUI调用Vsphere vcenter的 Web Service VSphere的Webservice地址默认为 ...

  3. tomcat-users.xml 配置

    一:tomcat6配置管理员信息 1:打开tomcat6下的~/conf/tomcat-users.xml文件,关于用户角色.管理员的信息都在这个配置文件中. 2:在配置文件<tomcat-us ...

  4. Jetson TX2安装固态硬盘(原创)

    SSD on Jetson TX2 注意事项:在断电情况下,将固态硬盘的接线与Jetson TX2进行连接 步骤: 一.jetson tx2开机,打开搜索栏中的Disks 二.Disks显示画面 三. ...

  5. sparse_softmax_cross_entropy_with_logits

    sparse_softmax_cross_entropy_with_logits 原创文章,请勿转载!!! 定义 sparse_softmax_cross_entropy_with_logits(_s ...

  6. MVC思想概述

    一. 传统Model1和Model2 Model1:整个web应用几乎全部用JSP页面组成,JSP页面接收处理客户端请求,对请求处理后直接作出响应.用少量的javaBean来处理数据库链接,数据库访问 ...

  7. C#常见问题总结(二)

    1.erp系统可以在具有固定ip的拥有多层服务器的局域网中使用吗?如何使用 解决方法: 把ini.配置文件字符串中的服务器名改成服务器的,把debug文件夹拷到其他机器上就行,服务器上的服务器名是默认 ...

  8. iOS-NSPredicate正则验证【三种验证方法】

    1.NSPredicate验证(谓词匹配) ///验证(string:验证的字符串) + (BOOL)stringValidate:(NSString *)string{ NSString *regu ...

  9. 最大流模版 dinic

    朴素dinic+多路增广 #include <iostream> #include <cstdio> #include <cstring> #include < ...

  10. Kitty猫基因编码

    原题链接:https://www.luogu.org/problemnew/show/2562#sub 简单的递归题.记录一下前缀和然后二分求解就好. 参考代码: #include <iostr ...