Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24205    Accepted Submission(s): 8537

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 1016 1010 1072 1241 
 

这题有很多坑点,

1朋友不止一个,所以r有多个,好解决,只要从a搜,找到第一个r结束即可

2警卫会消耗2点时间,但是队列是最先入队的, 最先入队的是步数最少的, 并不是时间最少的,所以必须要用优先队列,下面这段代码虽然AC但是是不正确的

//伪ac代码
#include<math.h>
#include<stdio.h>
#include<queue>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 1234
struct point
{
int x,y,t;
}st; int n,m;
int dx[]={,-,,};
int dy[]={,,,-};
char mat[N][N];
int vis[N][N]; int bfs()
{
queue<point>q;
q.push(st);vis[st.x][st.y]=;
while(!q.empty())
{
point cur=q.front();
q.pop();
for(int i=;i<;i++)
{
point next=cur;
next.x+=dx[i];next.y+=dy[i]; if(next.x<||next.x>n||next.y<||next.y>m)continue;
if(mat[next.x][next.y]=='#'||vis[next.x][next.y]==)continue;
if(mat[next.x][next.y]=='.')next.t=next.t+;
if(mat[next.x][next.y]=='x')next.t=next.t+;
if(mat[next.x][next.y]=='r')return next.t+; q.push(next);vis[next.x][next.y]=;
}
}
return -;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
scanf(" %c",&mat[i][j]);
if(mat[i][j]=='a')
st.x=i,st.y=j,st.t=;
}
int ans=bfs();
if(ans==-)puts("Poor ANGEL has to stay in the prison all his life.");
else cout<<ans<<endl;
}
return ;
}

但面对这组数据:

2 10
axxxxxxxxr
..........
结果就不对了

这个问题能够用优先队列解决。 详见代码

优先队列版:

#include<queue>
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define N 1234 int n, m, a, b, x, y, t, cnt, ans;
struct node
{
int x, y, time; bool operator < (const node & t)const
{
return time > t.time; //改成<号 则较大的先出队
}
}st;
priority_queue<node>q; //加上前缀 priority_ char mat[N][N];
int v[N][N];
int dx[]={, -, , };
int dy[]={, , , -}; int bfs()
{
memset(v , , sizeof(v));
while(!q.empty()) q.pop();
q.push(st);
v[st.x][st.y] = true;
while(!q.empty())
{
st = q.top(); //优先队列用q.top() 代替 q.front();
q.pop();
for(int i = ; i < ; i++)
{
node next = st;
next.x += dx[i], next.y +=dy[i];
if(v[next.x][next.y] || mat[next.x][next.y]=='#') continue;
if(next.x< || next.y< || next.x>n || next.y>m) continue;
if(mat[next.x][next.y] == '.')next.time++;
if(mat[next.x][next.y] == 'x')next.time+=;
if(mat[next.x][next.y] == 'r') return next.time+; q.push(next);
v[next.x][next.y] = true;
}
}
return -;
} int main()
{
while(cin>>n>>m)
{
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++)
{
scanf(" %c", &mat[i][j]);
if(mat[i][j] == 'a')
st.x = i, st.y = j, st.time = ;
} ans = bfs(); if(ans == -)puts("Poor ANGEL has to stay in the prison all his life.");
else cout<<ans<<endl; } return ;
} /*
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
2 10
axxxxxxxxr
..........
3 10
axxxxxxxxr
.########.
..........
*/
这个可以作为模板。

HDU 1242 rescue (优先队列模板题)的更多相关文章

  1. hdu 1242 Rescue

    题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...

  2. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  3. HDU 1251 Trie树模板题

    1.HDU 1251 统计难题  Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #incl ...

  4. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

  5. HDU 2896 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...

  6. hdu 1711 KMP算法模板题

    题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...

  7. HDU 2544 最短路(模板题)

    求1到N的最短路径,模板题,以1为源点,用dijkstra算法(可以用优先级队列优化) #include <iostream> #include <algorithm> #in ...

  8. Number Sequence - HDU 1711(KMP模板题)

    题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1   分析:应该是最简单的模板题了吧..... 代码如下: ==================== ...

  9. 【网络流#1】hdu 3549 - 最大流模板题

    因为坑了无数次队友 要开始学习网络流了,先从基础的开始,嗯~ 这道题是最大流的模板题,用来测试模板好啦~ Edmonds_Karp模板 with 前向星 时间复杂度o(V*E^2) #include& ...

  10. HDU 4825 Xor Sum (模板题)【01字典树】

    <题目链接> 题目大意: 给定n个数,进行m次查找,每次查找输出n个数中与给定数异或结果最大的数. 解题分析: 01字典树模板题,01字典树在求解异或问题上十分高效.利用给定数据的二进制数 ...

随机推荐

  1. js根据银行卡号判断属于哪个银行,并返回银行缩写及银行卡类型

      在做绑定银行卡,输入银行卡的时候,产品有这么一个需求,需要用户输入银行卡号的时候,显示对应的银行卡名称及简称.于是苦苦寻觅,终于找到了支付宝的开放API,银行卡校验接口 https://ccdca ...

  2. http过程

    当在浏览器里输入URL地址时,http的通讯过程: 1) 连接 DNS解析:URL——>DNS服务器(找到返回其ip,否则继续将DNS解析请求传给上级DNS服务器) Socket连接:通过IP和 ...

  3. linux 系统备份还原

    操作系统或文件备份 tar cvpzf backup.tgz --exclude=/proc --exclude=/lost+found --exclude=/backup.tgz --exclude ...

  4. Java-确定一个类对象代表一个类还是接口

    package com.tj; public class MyClass implements Cloneable { public static void main(String[] args) { ...

  5. 用Go编写的本地文件服务器

    本文来自网易云社区,转载务必请注明出处. 一.前言 一切问题的起源就是来自一个问题"为什么我打的jar包没有注解?",带着这个疑问查了一圈资料,原来问题主要是在没有将源码中的注释进 ...

  6. K-lord #2

    题目描述 还记得,高中数学联赛,2005,那道毒瘤题. 如果自然数的各位数字之和等于7,那么称为“吉祥数”.将所有“吉祥数”从小到大排成一列 $a_{1}$,$a_{2}$,$a_{3}$ ... , ...

  7. 【2018.8.10】四连测day4 题解

    T1:给出一棵 $n$ 个节点的无根树,其中 $m$ 个节点是特殊节点,求对于任意 $i ∈ [0, m]$,包含 $i$ 个特殊节点的联通块个数$\mod 998244353$. $1<=n, ...

  8. Spoj-NETADMIN Smart Network Administrator

    The citizens of a small village are tired of being the only inhabitants around without a connection ...

  9. Android Studio升级到3.0,抛出Aapt2Exception异常

    android studiao错误: Android resource linking failedOutput: D:\_ASWorkSpace\phone_new\app\src\main\res ...

  10. Lucene 6.5.0 入门Demo

    Lucene 6.5.0 要求jdk 1.8 1.目录结构: 2.数据库环境: private int id; private String name; private float price; pr ...