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. Net Core Docker

    Net Core Docker轻量级的web框架   .net core现在已经有了大的发展,虽然笔者现在已经从事python开发,但是一直在关注.net的发展,在逛博客园的时候,发现有大家都会提到N ...

  2. Codeforces Round #198 (Div. 2) —— D

    昨天想了一下D题,有点思路不过感觉很麻烦,就懒得去敲了: 今天上午也想了一下,还是没有结果,看了一下官方题解,证明得很精彩: 这道题目其实就是一道裸地最大上升子序列的题: 看到这里,直接怒码···· ...

  3. ubuntu 64bit “arm-linux-gcc: No such file or directory”问题的解决方法

    安装lsb-core sudo apt-get install lsb-core

  4. IntelliJ IDEA SVN的账号修改 信息清除

    来到编译器的setting设置 搜索subversion 点击subversion 找到下面的clear auth...按钮,点击一下 就可以了

  5. 【BZOJ 3476】 线段树===

    59  懒惰的奶牛贝西所在的牧场,散落着 N 堆牧草,其中第 i 堆牧草在 ( Xi,Yi ) 的位置,数量有 Ai 个单位.贝西从家移动到某一堆牧草的时候,只能沿坐标轴朝正北.正东.正西.正南这四个 ...

  6. 更好的 SQL 模式的 10 条规则

    更好的 SQL 模式的 10 条规则 2015-06-17 11:57:392353浏览1评论 在创建新表和数据仓库时,要做很多决定.一些在当时似乎无关紧要的地方,却让你和用户在数据库的生命期内感到痛 ...

  7. redis+PHP实现的一个优先级去重队列

    主要思路是用一个set做前端去重缓冲, 若干个list做后端的多优先级消息队列, 用一个进程来进行分发, 即从set中分发消息到队列. set缓冲的设计为当天有效, 所以有个零点问题,有可能在零点前s ...

  8. 转载:win7JDK环境配置

    [win7JDK环境配置] 网址:http://blog.sina.com.cn/s/blog_6a9df2330100ms9q.html 系统变量下: (1) 新建->变量名:JAVA_HOM ...

  9. C#反射之基础应用

    今天把反射的东西整理了一下 , 提供了最全面的东西 , 当然也是基础的东西 ,在学好了这一切的基础上 , 大家可以学习反射的具体插件等应用 首先我们建立一个类库 , 将它生成为 reflectPrj  ...

  10. maven 下载 源码和javadoc命令

    1:Maven命令下载源码和javadocs 当在IDE中使用Maven时如果想要看引用的jar包中类的源码和javadoc需要通过maven命令下载这些源码,然后再进行引入,通过mvn命令能够容易的 ...