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. css---一个大div中套左右两个div,如何让最高的把最低的撑开?且把父级撑开呢?

    到最后实现了效果,但是在理论上感觉还是很牵强,如果哪位大神有方法,请评论指出哦 Html: css:

  2. js 获取select的值

    var t = document.getElementById("provid"); console.log(t.value); console.log(t.text); //未定 ...

  3. html caption标签 语法

    html caption标签 语法 caption是什么标签? 作用:定义表格标题. 说明:caption 标签必须紧随 table 标签之后.您只能对每个表格定义一个标题.通常这个标题会被居中于表格 ...

  4. docker-compose进行部署容器的时候,报错权限不足

    刚刚用docker-compose部署elk的时候,没有起来,查看日志的时候,发现在数据卷挂载的时候,报错权限不足. 由于日志不在了,这里就直接贴出解决办法. 问题原因及解决办法 原因是CentOS7 ...

  5. 【C++】char* 和 char[] 区别的理解

    string 是一个类,其中有一个 char * 类型的私有变量. 因此可以如下构建一个string类型的变量. string str = "abcd"; 其中,右值“abcd”一 ...

  6. 2019.9.16JAVA课堂作业

    public class TestDouble {     public static void main(String args[]) {        System.out.println(&qu ...

  7. nbu还原集群数据库异常问题

    集群数据库软件均已安装完毕,现在想从NBU上还原数据库,但在还原控制文件报错 [oracle@oracle-db1 ~]$ rman target / Recovery Manager: Releas ...

  8. 【每日一包0002】array-first

    github地址:https://github.com/ABCDdouyae... array-first 获取数组的第一项或者前几项 文档地址:https://www.npmjs.com/packa ...

  9. DataList是外部传入的子项数据列表

    //定义适配器类public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{ private C ...

  10. 《Effective Java》读书笔记 - 9.异常

    Chapter 9 Exceptions Item 57: Use exceptions only for exceptional conditions 这条item的意思就是,千万不要用except ...