HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)
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
又是一道经典的深搜题,做了1180题再来做这个,就是秒A啊。
说下题意:
天使被关在了一个监狱里面,a代表天使,r代表天使的朋友,x是警卫,#是墙。r可以杀死所以的警卫,杀一个警卫需要1个单位时间,移动一步也需要一个单位时间。问,天使的朋友r最短需要多久才可以把天使a救出来。
如果不能救出天使,输出:Poor ANGEL has to stay in the prison all his life.
#include <iostream>
#include <cstdio>
#include <cstring>
#include<queue>
using namespace std;
struct node{
int x,y,t;
friend bool operator<(node n1,node n2){
return n2.t<n1.t;
}
};
int n,m;
char map[210][210];
int d[210][210];
int ax,ay,rx,ry;
node ft;
int mx[]={1,0,-1,0};
int my[]={0,-1,0,1};
int judge(int x,int y){
if(x<0||x>=n||y<0||y>=m){
return 0;
}
if(map[x][y]=='#'){
return 0;
}
if(d[x][y]){
return 0;
}
return 1;
}
void bfs(){
priority_queue<node> q;
ft.x=rx;
ft.y=ry;
ft.t=0;
q.push(ft);
d[rx][ry]=1;
while(!q.empty()){
ft=q.top();
q.pop();
//printf("%d,%d,%d\n",ft.x,ft.y,ft.t);
int x=ft.x;
int y=ft.y;
if(x==ax&&y==ay){
printf("%d\n",ft.t);
return;
}
for(int i=0;i<4;i++){
x=ft.x+mx[i];
y=ft.y+my[i];
if(!judge(x,y)){
continue;
}
node nt;
if(map[x][y]=='.'||map[x][y]=='a'){
nt.x=x;
nt.y=y;
nt.t=ft.t+1;
d[x][y]=1;
q.push(nt);
}else if(map[x][y]=='x'){
nt.x=x;
nt.y=y;
nt.t=ft.t+2;
d[x][y]=1;
q.push(nt);
}
}
}
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
int main()
{
while(~scanf("%d%d",&n,&m)){
memset(d,0,sizeof(d));
for(int i=0;i<n;i++){
scanf("%s",map[i]);
for(int j=0;j<m;j++){
if(map[i][j]=='a'){
ax=i,ay=j;
}
if(map[i][j]=='r'){
rx=i,ry=j;
}
}
}
bfs();
}
return 0;
}
HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)的更多相关文章
- hdu 1242:Rescue(BFS广搜 + 优先队列)
Rescue Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- hdu 1242 Rescue(bfs)
此刻再看优先队列,不像刚接触时的那般迷茫!这也许就是集训的成果吧! 加油!!!优先队列必须要搞定的! 这道题意很简单!自己定义优先级别! +++++++++++++++++++++++++++++++ ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
- HDU 1242 Rescue (BFS(广度优先搜索))
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- hdu 1242 Rescue(BFS,优先队列,基础)
题目 /******************以下思路来自百度菜鸟的程序人生*********************/ bfs即可,可能有多个’r’,而’a’只有一个,从’a’开始搜,找到的第一个’r ...
- HDOJ/HDU Tempter of the Bone(深搜+奇偶性剪枝)
Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...
- HDU 1242 Rescue (BFS+优先队列)
题意:X代表卫兵,a代表终点,r代表起始点,.代表路,#代表墙,走过.要花费一秒,走过x要花费2秒,求从起点到终点的最少时间. 析:一看到样例就知道是BFS了吧,很明显是最短路径问题,不过又加了一个条 ...
- hdu 1242 Rescue(BFS入门)
第一次用容器做的BFS题目,题目有个地方比较坑,就是遍历时的方向,比如上下左右能AC,右上左下就WA #include <stdio.h> #include <string.h> ...
- hdu 1242 Rescue
题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...
随机推荐
- 2014年9月21日_随笔,jdic,ETL,groovy,Nutz好多东西想学
(1)老妈十一要回老家,才突然发现买票好难啊.有亲朋很重要 (2)这周我做了什么.jdic,ETL,groovy, Nutz好多东西想学. Nutz开发成员专访.Nutz优酷视频(演讲).Nutz 入 ...
- 利用Xlinix SDK 建立Linux程序以及对该程序进行调试
一.创建Linux程序 1. 点击File > New > Application Project .并参照下图设置. 2. 输入工程名,并选择存储路径. 3. 选择所需的操作系统平台(O ...
- 安装hadoop多节点 各种整理
ubuntu烧制usb启动盘链接: 点击打开链接https://help.ubuntu.com/community/Installation/FromUSBStick ubuntu磁盘分区: 点击打开 ...
- js函数与变量同名
console.log(a); var a = 3; function a(){} 输出的结果是:[Function: a] 注意一下几点就能知道原因了! 1)函数声明会置顶2)变量声明也会置顶3)函 ...
- php重载
重载 PHP所提供的"重载"(overloading)是指动态地"创建"类属性和方法.我们是通过 魔术方法(magic methods)来实现的. 当调用当前环 ...
- NodeJS加MongoDB应用入门
OS:Windows 7 1.下载安装MongoDB:http://www.mongodb.org/downloads 2.下载安装NodeJS:http://nodejs.org/ 3.运行Mong ...
- STM32之SD卡
目录 一.SD卡概述 1.定义 2.容量等级 3.SD卡框图 4.SD卡与TF卡的区别 二. SD卡内部结构 1. SD卡内部结构简图 2. 存储阵列结构图 3.Buffer 4.“存储阵列Block ...
- Web负载均衡的几种方式
Web负载均衡的几种实现方式 摘要:负载均衡(Load Balance)是集群技术(Cluster)的一种应用.负载均衡可以将工作任务分摊到多个处理单元,从而提高并发处理能力.目前最常见的负载均衡应用 ...
- 单片微机原理P0:80C51结构原理
本来我真的不想让51的东西出现在我的博客上的,因为51这种东西真的太low了,学了最多就所谓的垃圾科创利用一下,但是想一下这门课我也要考试,还是写一点东西顺便放博客上吧. 这一系列主要参考<单片 ...
- cocos2dx3.4 导出节点树到XML文件
l利用cocostudio做UI和场景时,经常要去获取某个节点,cocostudio2.1开始加入了文件的概念,可以创建场景,节点,层等文件,把公用的东西创建到文件里,然后把这个文件拖到场景里使用,达 ...