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 题意:一个牢房图。#是墙壁,a是angel。r是angel的朋友。x是敌人,每走一步消耗1个单位时间。消灭1个敌人也消耗一个单位时间。求r到a的最小时间。 。 依照正常的思路:直接从R BFS 到 a 遇到 x就多加一个时间 即可了 只是这样是不正确了(题目太水 还是A了)
伪AC代码:
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
#define w 205
char map[w][w];
int vis[w][w];
int sx,sy;
int m,n;
struct node {
int x,y,time;
};
int fx[4][2]={1,0,0,1,-1,0,0,-1};
int bfs()
{
int tx,ty;
memset(vis,0,sizeof(vis));
node now,next;
queue<node>q;
now.x=sx;now.y=sy;now.time=0;
vis[sx][sy]=1;
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
if(map[now.x][now.y]=='r')
return now.time;
for(int i=0;i<4;i++)
{
tx=now.x+fx[i][0];
ty=now.y+fx[i][1];
if(tx<1||tx>m||ty<1||ty>n||vis[tx][ty]==1||map[tx][ty]=='#')
continue;
vis[tx][ty]=1;
next.x=tx;
next.y=ty;
if(map[tx][ty]=='x')
next.time=now.time+2;
else
next.time=now.time+1;
q.push(next);
}
}
return -1;
}
int main()
{
int i,j;
while(cin>>m>>n)
{
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
{
cin>>map[i][j];
if(map[i][j]=='a')
{
sx=i;sy=j;
}
}
int ss= bfs();
if(ss==-1)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",ss);
}
return 0;
}

但面对这组数据:
3 4

a...
##x.
###r

结果 却是6(应该是5啊)
原因事实上是(2,3) 和(1.4)是同步进入队列的 进入的顺序和方向数组有关= =

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

优先队列版:

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
#define w 205
char map[w][w];
int vis[w][w];
int sx,sy;
int m,n;
struct node {
int x,y,time;
bool operator <(const node & t) const
{
return time>t.time; //改成<号 则较大的先出队
}
};
int fx[4][2]={1,0,0,1,-1,0,0,-1};
int bfs()
{
int tx,ty;
memset(vis,0,sizeof(vis));
node now,next;
priority_queue<node>q; //加上前缀 priority_
now.x=sx;now.y=sy;now.time=0;
vis[sx][sy]=1;
q.push(now);
while(!q.empty())
{
now=q.top(); //优先队列不能用 q.front();
q.pop();
if(map[now.x][now.y]=='r')
return now.time;
for(int i=0;i<4;i++)
{
tx=now.x+fx[i][0];
ty=now.y+fx[i][1];
if(tx<1||tx>m||ty<1||ty>n||vis[tx][ty]==1||map[tx][ty]=='#')
continue;
vis[tx][ty]=1;
next.x=tx;
next.y=ty;
if(map[tx][ty]=='x')
next.time=now.time+2;
else
next.time=now.time+1;
q.push(next);
}
}
return -1;
}
int main()
{
int i,j;
while(cin>>m>>n)
{
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
{
cin>>map[i][j];
if(map[i][j]=='a')
{
sx=i;sy=j;
}
}
int ss= bfs();
if(ss==-1)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",ss);
}
return 0;
}

模板。

。。

 

 

版权声明:本文博主原创文章。博客,未经同意不得转载。

HDU 1242 rescue and 优先级队列的条目的更多相关文章

  1. hdu 1242 Rescue

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

  2. hdu 1242:Rescue(BFS广搜 + 优先队列)

    Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  3. HDU 1242 Rescue(BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...

  4. hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...

  5. hdu 1242 Rescue(BFS,优先队列,基础)

    题目 /******************以下思路来自百度菜鸟的程序人生*********************/ bfs即可,可能有多个’r’,而’a’只有一个,从’a’开始搜,找到的第一个’r ...

  6. hdu 1242 Rescue(bfs)

    此刻再看优先队列,不像刚接触时的那般迷茫!这也许就是集训的成果吧! 加油!!!优先队列必须要搞定的! 这道题意很简单!自己定义优先级别! +++++++++++++++++++++++++++++++ ...

  7. 杭电 HDU 1242 Rescue

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 问题:牢房里有墙(#),警卫(x)和道路( . ),天使被关在牢房里位置为a,你的位置在r处,杀死一个警卫 ...

  8. HDU 1242 Rescue(优先队列)

    题目来源: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description   Angel was caught by ...

  9. HDU 1242 rescue (优先队列模板题)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

随机推荐

  1. jsp EL表达式 字符串的比较

    jsp EL表达式 字符串的比较 跟JavaScript一样,直接使用两个等于号即可:== 代码如下: <c:if test="${highLight == 'visa'}" ...

  2. 解决CAS单点登录出现PKIX path building failed的问题

    在一次调试中,出现了这个错误: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderExceptio ...

  3. 被忽视的eMMC——A1 SD Bench闪存测试

    一直以来,大家对手机的配置方面都比较关注CPU和GPU的架构.频率.核心数等,却经常忽略了手机闪存的速度.实际上手机的闪存素质对手机日常操作的响应.载入速度同样起到举足轻重的影响,今天给大家介绍的则是 ...

  4. 猜测:信号槽的本质是使用Windows的自定义消息来实现的

    在不断执行: void MyTool::DeleteAllFiles(){ for (i = 0; i <= n - 1; i++) { // do something }}在for循环没有执行 ...

  5. C++解析JSON之JsonCPP

    一.JSON简介 JSON全称为JavaScript ObjectNotation,它是一种轻量级的数据交换格式,易于阅读.编写.解析. JSON由两种基本结构构成: )"名称/值" ...

  6. wpf 创建动画三种方式

    动画类型 : 故事版,CompositionTarget,DispachTime 那么到此,三种动态创建动画的方法都已经详细介绍过了,大家可能会有种感觉,比较钟情于第一种WPF/Silverlight ...

  7. SYS_CONTEXT 详细用法

    SELECT SYS_CONTEXT ('USERENV', 'TERMINAL') terminal,        SYS_CONTEXT ('USERENV', 'LANGUAGE') lang ...

  8. 在windows下解压缩Linux内核源代码出现重复文件原因

    在windows下解压缩Linux内核源代码出现重复文件原因 2009年06月30日 13:35 来源:ChinaUnix博客 作者:embededgood 编辑:周荣茂     原因一.因为在Lin ...

  9. MySQL源码 解析器

    sql请求发送到server端,需要经过解析器生成内部的数据结构对象,以方便进行优化和生成执行计划.解析器主要做了两件事情,词法分析和语法分析. 词法和语法分析:mysql使用lex词法分析器,yac ...

  10. BZOJ3522: [Poi2014]Hotel

    3522: [Poi2014]Hotel Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 195  Solved: 85[Submit][Status] ...