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. 关于python pip安装第三方库 jieba 中文分词工具后提示"ImportError: cannot import name 'Random'"报错问题

    具体错误提示如下: >>> import jieba Traceback (most recent call last): File "<stdin>" ...

  2. luogu P1077 摆花 x

    P1077 摆花 题目描述 小明的花店新开张,为了吸引顾客,他想在花店的门口摆上一排花,共m盆.通过调查顾客的喜好,小明列出了顾客最喜欢的n种花,从1到n标号.为了在门口展出更多种花,规定第i种花不能 ...

  3. (57)Linux驱动开发之三Linux字符设备驱动

    1.一般情况下,对每一种设备驱动都会定义一个软件模块,这个工程模块包含.h和.c文件,前者定义该设备驱动的数据结构并声明外部函数,后者进行设备驱动的具体实现. 2.典型的无操作系统下的逻辑开发程序是: ...

  4. Jprofiler远程监控JVM

    一.下载并安装 本地和远程服务器分别安装Jprofiler,下载地址 二.Windows远程连接JVM配置 1.打开Windows客户端Jprofiler 2.点Cancel 3.创建远程会话 4.添 ...

  5. arguments详解——函数内命名参数之映射

    首先,arguments对象是所有(非箭头)函数中都可用的局部变量.你可以使用arguments对象在函数中引用函数的参数.此对象包含传递给函数的每个参数,第一个参数在索引0处. arguments对 ...

  6. SPFA算法的判负环问题(BFS与DFS实现)

    经过笔者的多次实践(失败),在此温馨提示:用SPFA判负环时一定要特别小心! 首先SPFA有BFS和DFS两种实现方式,两者的判负环方式也是不同的.       BFS是用一个num数组,num[x] ...

  7. Promethus

    https://blog.csdn.net/zl1zl2zl3/article/details/74332437

  8. React Native商城项目实战04 - 封装TabNavigator.Item的创建

    1.Main.js /** * 主页面 */ import React, { Component } from 'react'; import { StyleSheet, Text, View, Im ...

  9. python - 标准库:subprocess模块

    subprocess的目的就是启动一个新的进程并且与之通信. subprocess模块中只定义了一个类: Popen. subprocess.Popen(args, bufsize=0, execut ...

  10. 测开之路一百五十五:jquery-validation前台数据验证

    前面做的wtform验证是服务器端的验证,需要把数据传输到服务器,服务器验证后再吧结果传输到前端,网络慢的时候,用户体验不好,所以需要前端验证,且后端验证不能少 传统的js或者jquery如果要验证信 ...