直接把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. CColor类封装

    CColor类封装 Color.h #pragma once #include <sstream> #include <string> using namespace std; ...

  2. JavaScript之点赞特效

    在很多网站中登录后可以对你说喜欢的文章.图片之类的进行点赞,这个功能效果的实现就是我今天要写的东西.代码不长,但觉得还是很有用的,为 js 给用户交互体验上更加友好.在实现过程中,主要的思路有: 点赞 ...

  3. C语言学习之选择排序

    上一篇文章中讲C语言排序中的比较常见的(交换)冒泡排序,那么这篇文章也将以新手个人的经历来讲同样比较常见而实用的数组排序之选择排序. 选择排序,从字面上看是通过选择来进行排序.其实它的用法就是通过选择 ...

  4. find与tar的结合使用

    新建一个文件,自定义时间点[root@nhserver2 ~]# touch -t 1403010000.00 file1.txt   新建一个文件,自定义时间点[root@nhserver2 ~]# ...

  5. Centos7.0关闭防火墙

    CentOS 7.0默认使用的是firewall作为防火墙,使用iptables必须重新设置一下 1.直接关闭防火墙 systemctl stop firewalld.service #关闭firew ...

  6. window对象的属性

    还不是很了解,以后补充frames      opener       parent

  7. 面向切面编程之手动JDK代理方式

    需求描述: 抽取dao层开启和提交事物交由代理类一并执行 分析: 假如UserDao接口中有很多方法,例如addUser().deleteUser().updateUser()等等,需要频繁的和数据库 ...

  8. Linux中fdisk硬盘分区方法

    fdsik 能划分磁盘成为若干个区,同时也能为每个分区指定分区的文件系统,比如linux .fat32. linux .linux swap .fat16 以及其实类Unix类操作系统的文件系统等:当 ...

  9. vsftpd安装和使用 Linux系统和window系统

    vsftpd 安装(Linux)一.安装系统环境 centos 6.9 64位二.vsftpd版本 vsftpd-2.2.2-24.el6.x86_64三.安装步骤1.安装 执行 yum -y ins ...

  10. JDBC为什么要使用PreparedStatement而不是Statement

    PreparedStatement是什么? PreparedStatement是java.sql包下面的一个接口,用来执行SQL语句查询,通过调用connection.preparedStatemen ...