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. matlab中 hold on 与hold off的用法

    matlab中 hold on 与hold off的用法 hold on 是当前轴及图形保持而不被刷新,准备接受此后将绘制 hold off 使当前轴及图形不在具备被刷新的性质 hold on 和ho ...

  2. underscore demo

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  3. 盘点 PHP 和 ASP.NET 的10大对比!

    [编者按]本文主要针对开源 PHP 和非开源的 ASP.NET 在性能.成本.可扩展性,技术支持和复杂性等方面进行比较. 在网上论坛,总是有成百上千的文章和帖子在讨论 PHP 和 ASP.NET,究竟 ...

  4. UITableView.m:8042 crash 崩溃

     CRASH : /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.6.21/UITableView.m:804 ...

  5. SPRING IN ACTION 第4版笔记-第三章Advancing wiring-001-DataSource在应用和开发环境之间切换 profile

    一. DataSource在应用和开发环境的产生方式不同,可以用srping 的profile管理 Spring’s solution for environment-specific beans i ...

  6. [NYOJ 860] 又见01背包

    又见01背包 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述     有n个重量和价值分别为wi 和 vi 的 物品,从这些物品中选择总重量不超过 W  的物品,求所 ...

  7. DataSet用法详细

    转自:http://www.cnblogs.com/zeroone/archive/2012/06/08/2541299.html DataSet用法详细 一.特点介绍 1.处理脱机数据,在多层应用程 ...

  8. calabash-android Win10 入门笔记

    参考官方文档:https://developer.xamarin.com/guides/testcloud/calabash/   概述     Calabash是一个BDD的UI自动化验收测试框架, ...

  9. 作品第二课----点击DIV显示其内容

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. [JLOI2013]卡牌游戏

    [题目描述 Description] N个人坐成一圈玩游戏.一开始我们把所有玩家按顺时针从1到N编号.首先第一回合是玩家1作为庄家.每个回合庄家都会随机(即按相等的概率)从卡牌堆里选择一张卡片,假设卡 ...