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. 编译caffe-gpu-cuda及cudnn-tar 下载地址

    y下载 https://github.com/BVLC/caffe https://github.com/BVLC/caffe/archive/master.zip gcc caffe安装 有2个问题 ...

  2. Devexpress MVC Gridview 获取到增删改的所有行数据(JSON) 并使用SQL事物保存数据

    //ModalChargeGridView Gridview的名字//Con_Shp_Chg 数据库表名//ConShpChgUID UID或者是标识列//gs_Language 语言(中英文)//l ...

  3. 【bzoj1026】[SCOI2009]windy数

    *题目描述: windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道,在A和B之间,包括A和B,总共有多少个windy数? *输入: 包含 ...

  4. 【CodeChef】LECOINS(同余最短路,背包DP)

    题意:给定n个物品,每个物品可以取无限次,每个物品有两种属性:价值v和颜色c 现在有q个询问,每次询问是否能取出价值和为S的方案,如有多解输出不同颜色种数的最大值 题意:看到BZOJ评论区有好心人说C ...

  5. 通过PPA存储库在UBUNTU或LINUX MINT中安装ORACLE JAVA 8 [JDK8]

    http://www.webupd8.org/2012/09/install-oracle-java-8-in-ubuntu-via-ppa.html sudo add-apt-repository ...

  6. 解析获得的网页数据(XML文件或JSON文件)

    1.解析XML:使用Pull方式. 需要导入jar包:xmlpull-xpp3-1.1.4c.jar //Pull解析XML文件 private void parseXMLWithPull(Strin ...

  7. 使用注解装配Bean

    注解@Component代表Spring Ioc 会把 这个类扫描生产Bean 实例,而其中 value属性代表这个类在Spring 中的id,这就相当于XML方式定义的Bean  的 id 现在有了 ...

  8. mysql修改数据表某列的配置

    alter table 表名 modify column 字段名 类型;alter table 表名 drop column 字段名

  9. sh脚本获取当前目录

    #!/bin/bashcurDir=$(pwd)echo "cur dir is:$curDir"

  10. Docker追加容器端口映射

    参考文章: https://blog.csdn.net/chouzhou9701/article/details/86725203