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. HTTP与HTTPS区别(详细)

    转:http://blog.sina.com.cn/s/blog_6eb3177a0102x66r.html 1.减少http请求(合并文件.合并图片)2.优化图片文件,减小其尺寸,特别是缩略图,一定 ...

  2. Codeforces Round #303 (Div. 2) E. Paths and Trees Dijkstra堆优化+贪心(!!!)

    E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  3. 微信小程序POST请求参数传递不到后台, 前台获取不到后端返回的数据, 以及 post 请求返回 404 但后台能收到数据

    1 微信小程序POST请求参数传递不到后台 需要在微信请求 wx.request 改变默认 header 配置为如下 wx.request({ url: 'test.php', //仅为示例,并非真实 ...

  4. 随堂小测APP使用体验

    随堂小测APP使用体验 先要去注册账号需要填写用户名.密码.手机号.学号/教师号.学校.专业.即可注册,注册成功后,即可登录APP进,登陆进去以后.会有两个界面,课堂和我的,注册.登录简单,通俗易懂, ...

  5. Android中实现双击(多击)事件

    要实现双击,你需要保存第一次点击时的时间,需要使用到变量,之后便是与第二次点击时的时间比较,看时间间隔是否在你设定的时间内(比如500ms). ? 1 2 3 4 5 6 7 8 9 10 11 12 ...

  6. SQL Server 2012 sa 用户登录 18456 错误 (转)

    转自:http://blog.csdn.net/waterxcfg304/article/details/40617475 最近想研究下SQL SERVER2012 Enterprise版本的数据库, ...

  7. WKWebView使用指南|功能丰富的JXBWKWebView

    github地址:JXBWKWebView,如果觉得项目不错可以点个star支持一下,谢谢~ 前言 目前iOS系统已经更新到iOS11,大多数项目向下兼容最多兼容到iOS8,因此,在项目中对WebVi ...

  8. jprofiler监控wls&was配置

    jprofiler简介: jprofiler的内存视图部分可以提供动态的内存使用状况更新视图和显示关于内存分配状况信息的视图.所有的视图都有几个聚集层并且能够显示现有存在的对象和作为垃圾回收的对象. ...

  9. 通过helm 安装 harbor 不成功问题处理

    helm 安装 harbor 安装不成功处理过程 通过安装脚本 helm install c7n/harbor \ --set expose.ingress.hosts.core=harbor.ls. ...

  10. c语言中static关键字用法详解

    个人总结: 1.C不是面向对象的,在c中static修饰的变量或函数仅在当前文件中使用 2.C可以对局部变量使用static修饰(注意面向对象的java则不行),其放在全局区一直存在 概述static ...