Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 31648    Accepted Submission(s): 11083


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
 

Source
 

Recommend
Eddy   |   We have carefully selected several similar problems for you:  1240 1072 1253 1175 1548 




这次被困住的是天使,不是公主了,但是该救还是得救,还是BFS;
代码如下:
Problem : 1242 ( Rescue )     Judge Status : Accepted

RunId : 21282960    Language : G++    Author : hnustwanghe

Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>

using namespace std;
const int N = 200 + 5;
char mat[N][N];
bool visit[N][N];

typedef struct node{
int x,y,val;
bool operator < (const node x)const {
return val > x.val;
}
}Node;
int goalx,goaly,startx,starty,n,m;
const int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};

int BFS(){
priority_queue<Node> Q;
memset(visit,0,sizeof(visit));
Node t,s;
int newx,newy;
visit[startx][starty] = true;
Q.push((Node){startx,starty,0});
while(!Q.empty()){
t = Q.top();Q.pop();
if(t.x == goalx && t.y == goaly ) return t.val;
for(int d=0;d<4;d++){
newx = t.x + dir[d][0];
newy = t.y + dir[d][1];
if(!visit[newx][newy] && newx>=0 && newx<n && newy>=0 && newy < m && mat[newx][newy]!='#'){
int val = (mat[newx][newy]=='x')?2:1;
s.x = newx,s.y= newy,s.val = t.val + val;
visit[newx][newy] = true;
Q.push(s);
}
}
}
return -1;
}

int main(){
while(scanf("%d %d",&n,&m)==2){
for(int i=0;i<n;i++){
scanf("%s",mat[i]);
for(int j=0;j<m;j++)
if(mat[i][j]=='r')
startx = i,starty = j;
else if(mat[i][j]=='a')
goalx = i,goaly = j;
}
int ans = BFS();
if(ans < 0)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",ans);
}
}

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>

using namespace
std;
const int
N = 200 + 5;
char
mat[N][N];
bool
visit[N][N]; typedef struct node{
int
x,y,val;
bool operator < (const
node x)const {
return
val > x.val;
}
}
Node;
int
goalx,goaly,startx,starty,n,m;
const int
dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; int BFS(){
priority_queue<Node> Q;
memset(visit,0,sizeof(visit));
Node t,s;
int
newx,newy;
visit[startx][starty] = true;
Q.push((Node){startx,starty,0});
while(!
Q.empty()){
t = Q.top();Q.pop();
if(
t.x == goalx && t.y == goaly ) return t.val;
for(int
d=0;d<4;d++){
newx = t.x + dir[d][0];
newy = t.y + dir[d][1];
if(!
visit[newx][newy] && newx>=0 && newx<n && newy>=0 && newy < m && mat[newx][newy]!='#'){
int
val = (mat[newx][newy]=='x')?2:1;
s.x = newx,s.y= newy,s.val = t.val + val;
visit[newx][newy] = true;
Q.push(s);
}
}
}
return -
1;
} int main(){
while(
scanf("%d %d",&n,&m)==2){
for(int
i=0;i<n;i++){
scanf("%s",mat[i]);
for(int
j=0;j<m;j++)
if(
mat[i][j]=='r')
startx = i,starty = j;
else if(
mat[i][j]=='a')
goalx = i,goaly = j;
}
int
ans = BFS();
if(
ans < 0)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else

printf("%d\n",ans);
}
}

 

搜索专题: HDU1242 Rescue的更多相关文章

  1. HDU(搜索专题) 1000 N皇后问题(深度优先搜索DFS)解题报告

    前几天一直在忙一些事情,所以一直没来得及开始这个搜索专题的训练,今天做了下这个专题的第一题,皇后问题在我没有开始接受Axie的算法低强度训练前,就早有耳闻了,但一直不知道是什么类型的题目,今天一看,原 ...

  2. NOIP2018提高组金牌训练营——搜索专题

    NOIP2018提高组金牌训练营——搜索专题 1416 两点 福克斯在玩一款手机解迷游戏,这个游戏叫做”两点”.基础级别的时候是在一个n×m单元上玩的.像这样: 每一个单元有包含一个有色点.我们将用不 ...

  3. 搜索专题:Balloons

    搜索专题:Balloons 这道题一看与时间有关,第一想到的就是BFS,定义一个状态,包含每一个状态的剩余气球数,已经进行的时间和每一个志愿者上一次吹气球的时间: 每一次状态转换时,检查是否有没有使用 ...

  4. hdu1242 Rescue DFS(路径探索题)

    这里我定义的路径探索题指 找某路能够到达目的地,每次走都有方向,由于是探索性的走 之后要后退 那些走过的状态都还原掉 地址:http://acm.hdu.edu.cn/showproblem.php? ...

  5. 2014 UESTC暑前集训搜索专题解题报告

    A.解救小Q BFS.每次到达一个状态时看是否是在传送阵的一点上,是则传送到另一点即可. 代码: #include <iostream> #include <cstdio> # ...

  6. HDU1242 Rescue

    Rescue Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Description A ...

  7. 【PHP高效搜索专题(2)】sphinx&coreseek在PHP程序中的应用实例

    PHP可以通过三种途径来调用sphinx 通过Sphinx官方提供的API接口(接口有Python,Java,Php三种版本) 通过安装SphinxSE,然后创建一个中介sphinxSE类型的表,再通 ...

  8. 【PHP高效搜索专题(1)】sphinx&Coreseek的介绍与安装

    我们已经知道mysql中带有"%keyword%"条件的sql是不走索引的,而不走索引的sql在大数据量+大并发量的时候,不仅效率极慢还很有可能让数据库崩溃.那我们如何通过某些关键 ...

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

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

随机推荐

  1. 8. ClustrixDB 监控

    一. 列出集群中当前会话 sql> select * from system.sessions\G 二. 显示CPU利用率.磁盘读/写利用率和缓冲区缓存失误率 MySQL [system]> ...

  2. jquery eq()选择器 语法

    jquery eq()选择器 语法 作用::eq() 选择器选取带有指定 index 值的元素.index 值从 0 开始,所有第一个元素的 index 值是 0(不是 1).经常与其他元素/选择器一 ...

  3. 将Emacs Org mode用于GTD任务管理

    在上一篇日志中,我简要介绍了如何围绕Emacs Org mode构建个人任务管理系统的基本思路与方法.因为Org mode体系庞大.功能繁杂,本文仅以提纲契领的方式介绍不同环节在Org mode中的操 ...

  4. opengl中相关的计算机图形变换矩阵之:模型视图几何变换

    3. 二维变换矩阵 x'      a11 a12 a13    x         a11x a12y a13z y' =  a21 a22 a23     y    =  a21x a22y a2 ...

  5. windows下开启远程连接Mysql

    使用“Ctrl + R”组合键快速打开cmd窗口,并输入“cmd”命令,打开cmd窗口.  使用“mysql -uroot -proot”命令可以连接到本地的mysql服务.  使用“use mysq ...

  6. 大数据笔记(二十八)——执行Spark任务、开发Spark WordCount程序

    一.执行Spark任务: 客户端 1.Spark Submit工具:提交Spark的任务(jar文件) (*)spark提供的用于提交Spark任务工具 (*)example:/root/traini ...

  7. 关于Tomcat重启和关闭后重启session变化

    ,当页面第一次访问,session的attribute还未赋值,为null 当页面第二次访问时,这时当前的session的attribute有值了! 到了本文章的点题时刻!! 如果我是直接点击serv ...

  8. IE与safari浏览器中时间格式问题

    一.问题内容 在js中处理Date时,发现IE与Safari和其他浏览器的支持方式不一致 1.例如:2017-01-01 12:00:00 在其他浏览器中,使用这个格式的字符串进行new Date操作 ...

  9. 关于Qt 构建套件(Kit) 编译器 自动识别不正确 不能修改的问题

    当系统内有多个Qt的版本的时候,QtCreater会自动识别出 编译器,Qt版本,和构建套件(Kit),但是有时候会发现,识别出来的路径不对,而且不能修改. 当出现这是问题的时候,可以到 qtcrea ...

  10. Python For Mac 开发环境安装 以及问题记录

    Python For Mac 开发环境安装记录 把自己安装的过程记录一下,亲测可用 1.Python3环境安装(转载http://www.cnblogs.com/meng1314-shuai/p/90 ...