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. 深入浅出Oracle:DBA入门、进阶与诊断案例(读书笔记2)

    第5章  Buffer Cache与Shared Pool原理 5.1 Buffer Cache原理 Buffer Cache是Oracle SGA中的一个重要部分,通常的数据访问和修改都需要通过Bu ...

  2. 关于awk中NR、FNR、NF、$NF、FS、OFS的说明

    一.NR和FNR 1.释义 NR: 表示当前读取的行数 FNR:当前修改了多少行 2.举例 比如现在AWK处理到第五行.第一行没有进行操作,2,3,4,5行进行了操作,那么NR=5,FNR=4 NR= ...

  3. 【11】把 GitHub 当 CMS 用

    把 GitHub 当 CMS 用 你的网站需要显示一些文字,但是你还不想直接放在 HTML 里面,那你可以把 GitHub 作为你储存内容的一个地方. 这样,就可以让任何一个非程序员通过修改 Mark ...

  4. NOS跨分区灾备设计与实现

    本文来自网易云社区 作者:王健 摘要 NOS(网易对象存储)在实现多机房(杭州机房,北京机房等)部署后,允许一个用户在建桶时选择桶所属机房.在此基础上,我们实现了跨机房的数据复制,进一步实现了跨机房的 ...

  5. 九度oj 题目1029:魔咒词典

    题目描述:     哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔咒,所以他需要你的帮 ...

  6. asp.net mvc 页面内容呈现Html.Raw HtmlString

    asp.net mvc 页面内容呈现Html.Raw Html.Raw内容经过页面呈现,不呈现Html标签 @Html.Raw( File.ReadAllText(Server.MapPath(&qu ...

  7. uva 550 有趣的乘法(dfs)

    题目大意:给三个数A(进制).B(如*****7的最后一个数字7).C(*****7*4的后面的因数4)求符合条件下的第一个因数的位数最少 例子: 179487 * 4 = 717948 (10进制) ...

  8. Gty的妹子树(bzoj 3720)

    Description 我曾在弦歌之中听过你, 檀板声碎,半出折子戏. 舞榭歌台被风吹去, 岁月深处尚有余音一缕…… Gty神(xian)犇(chong)从来不缺妹子…… 他来到了一棵妹子树下,发现每 ...

  9. 窗口(codevs 4373)

    题目描述 Description 给你一个长度为N的数组,一个长为K的滑动的窗体从最左移至最右端,你只能见到窗口的K个数,每次窗体向右移动一位,如下表: Window position Min val ...

  10. 洛谷 P1095 守望者的逃离

    题目描述 恶魔猎手尤迪安野心勃勃,他背叛了暗夜精灵,率领深藏在海底的娜迦族企图叛变.守望者在与尤迪安的交锋中遭遇了围杀,被困在一个荒芜的大岛上.为了杀死守望者,尤迪安开始对这个荒岛施咒,这座岛很快就会 ...