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. Android Camera 预览图像被拉伸变形的解决方法【转】

    问题描述: 预览图像被拉伸变形 问题原因: 由于预览图像大小跟SurfaceView 大小不一致引起 解决方法: 获取系统支持的所有预览尺寸[getSupportedPictureSizes],然后再 ...

  2. java 常用基本数据类型的默认值

    在使用基本数据类型作为类成员的时候,有时只初始化了而没有给变量赋值,那么此时,java会给你的变量赋一个默认初始值. boolean        false char              '/ ...

  3. bzoj 3784: 树上的路径 堆维护第k大

    3784: 树上的路径 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 88  Solved: 27[Submit][Status][Discuss] ...

  4. js template

    http://garann.github.io/template-chooser/ http://www.gbin1.com/technology/javascript/20120917-javasc ...

  5. SDUT 2352 Run Length Encoding

    点我看题目 题意 :将给定的字符串编码,编码的规则根据两条,1.如果字符串里有连续相等的字符,就变为两个字符,一个是这些连续相同的字符的个数,另一个是这个字符,但是如果数量超过了9个,那就输出9再输出 ...

  6. 【UVA 1411】 Ants (KM)

    Young naturalist Bill studies ants in school. His ants feed onplant-louses that live on apple trees. ...

  7. 裸眼3D立体显示技术原理详解

    众所周知,现实世界是一个三维空间,除去时间这一维度,现实世界是由长度.宽度和高度三个维度组成,我们每天就生活在这个三维世界中,而现有的显示设备大多数都只能显示二维信息,并不能带给人真实的三维感觉.为了 ...

  8. ArrayList集合排序

    using System;using System.Collections;using System.Collections.Generic;using System.Text; namespace ...

  9. DBcontext应用于已存在数据库

    转自:http://www.cnblogs.com/Tally/archive/2013/01/30/2882855.html EF4.1有三种方式来进行数据操作及持久化.分别是Database-Fi ...

  10. Color the ball----HDOJ1556

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...