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' 代表警卫(耗 ...
随机推荐
- 使用hwclock同步RTC(硬件时钟)和OS Clock(操作系统时钟)
Linux下面有个命令叫做hwclock,其主要的功能是用来在RTC和操作系统之间进行时钟同步.既可以将RTC的时钟同步到操作系统,也可以在通过hwclock将当前系统时间.Internet时间.本地 ...
- Hash函数的安全性
我们为了保证消息的完整性,引进了散列函数,那么散列函数会对安全正造成什么影响呢?这是需要好好研究一番的问题. 三个概念: 1.如果y<>x,且h(x)=h(y),则称为碰撞. 2.对于给定 ...
- python 自动化之路 day 面向对象基础
1.面向對象基础概述 面向过程: 根据业务逻辑从上到下垒代码(如果程序修改,对于依赖的过程都需要进行修改.) 函数式: 将某功能代码封装到函数中,如后便无需重复编写,仅需要调用函数即可 面向对象: 世 ...
- jQuery DataTables 插件使用笔记
初始化 在页面中 <!DOCTYPE html> <html> <head> <link rel="stylesheet" type=&q ...
- sae后台管理端的js,daterangepicker使用
原本只为了日期范围选择器看下sae的前端怎么实现 然后... 公共函数两个文件,第一个是各种插件: typeahead.js 自动完成 //关键词自动完成 $('#page-auto-complete ...
- 【python】aassert 断言
语法 : assert 3>4 结果Traceback (most recent call last): File "<pyshell#0>", line 1, ...
- NOIP2015 普及组(Junior) 解题报告
1. 金币 (coin.cpp/c/pas) 国王将金币作为工资,发放给忠诚的骑士.第一天,骑士收到一枚金币:之后两天(第二天和第三天),每天收到两枚金币:之后三天(第四.五.六天),每天收到三枚金币 ...
- mysql-5.5.25-winx64在win7 x64 免安装配置
os:win7 x64 mysql:mysql-5.5.25-winx64 将mysql-5.5.25-winx64.zip 解压缩到F:\mysql-5.5.25-winx64 目录下: 1.将my ...
- 定位 - CoreLocation - INTULocationManager
https://github.com/intuit/LocationManager #import "ViewController.h" #import "INTULoc ...
- FFMPEG视音频编解码零基础学习方法-b
感谢大神分享,虽然现在还看不懂,留着大家一起看啦 PS:有不少人不清楚“FFmpeg”应该怎么读.它读作“ef ef em peg” 0. 背景知识 本章主要介绍一下FFMPEG都用在了哪里(在这里仅 ...