[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue

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

ZOJ Monthly, October 2003

解题思路:

一个地图中,给定起点和终点,上下左右走,每走一步花1个单位时间,遇到怪,需要杀死怪,额外花1个单位时间,求从起点到终点最少花多少分钟。

主要思路是广度优先搜索,但因为其中有怪,需要额外花时间,那么对于队列里面的每个节点(保存x,y坐标和当前所用时间),并不是“公平”的,要想最少花时间,那么每次在队头取出的元素应该是队列里面所用时间最少的,所以应该用优先队列。

代码:

#include <iostream>
#include <queue>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn=210;
char mp[maxn][maxn];
bool vis[maxn][maxn];
int dx[4]={0,1,-1,0};
int dy[4]={-1,0,0,1};
int n,m;//地图大小
int sx,sy;//起点
int ex,ey;//终点

struct Node
{
    int x,y;
    int step;
}node;

bool operator<(Node a,Node b)//定义结构体类型的优先队列的优先级,step小的优先
{
    return a.step>b.step;
}

void getMap(int n,int m)
{
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            cin>>mp[i][j];
            if(mp[i][j]=='r')
            {
                sx=i;
                sy=j;
            }
            if(mp[i][j]=='a')
            {
                ex=i;
                ey=j;
            }
        }
}
bool judge(int x,int y)//判断x,y是否可以到达
{
    if(x>=1&&x<=n&&y>=1&&y<=m&&!vis[x][y]&&mp[x][y]!='#')
        return true;
    return false;
}

int bfs(int x,int y)
{
    memset(vis,0,sizeof(vis));
    priority_queue<Node>q;
    Node a,b;
    a.x=x,a.y=y,a.step=0;
    q.push(a);
    while(!q.empty())
    {
        b=q.top();
        q.pop();
        for(int i=0;i<4;i++)
        {
            int nextx=b.x+dx[i];
            int nexty=b.y+dy[i];
            if(judge(nextx,nexty))
            {
                vis[nextx][nexty]=1;
                a.x=nextx;
                a.y=nexty;
                if(mp[nextx][nexty]=='x')
                   {
                       a.step=b.step+2;
                       q.push(a);
                   }
                else
                   {
                       a.step=b.step+1;
                       q.push(a);
                       if(nextx==ex&&nexty==ey)//找到就返回 不用接着找了
                           return a.step;
                   }
            }
        }
    }
    return -1;
}

int main()
{
    while(cin>>n>>m)
    {
        getMap(n,m);
        int ans=bfs(sx,sy);
        if(ans==-1)//没有访问终点
            cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
        else
            cout<<ans<<endl;
    }
    return 0;
}

[ACM] hdu 1242 Rescue (BFS+优先队列),布布扣,bubuko.com

时间: 2024-10-19 20:18:40

[ACM] hdu 1242 Rescue (BFS+优先队列)的相关文章

hdu 1242 Rescue(bfs+优先队列)

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

HDU 1242——Rescue(优先队列)

题意: 一个天使a被关在迷宫里,她的许多小伙伴r打算去救她,求小伙伴就到她需要的最小时间.在迷宫里有守卫,打败守卫需要一个单位时间,如果碰到守卫必须要杀死他 思路: 天使只有一个,她的小伙伴有很多,所以可以让天使找她的小伙伴,一旦找到小伙伴就renturn.时间小的优先级高.优先队列搞定 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<

hdu 1242 rescue (优先队列 bfs)

题意: 公主被关在 a位置 她的朋友在r位置 路上x位置有恶魔 遇上恶魔花费2 时间 否在时间花费 1 时间 问 最短多少时间 找到公主 思路: bfs+ 优先队列(时间短的先出列) #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<iostream> #include<algorithm> using namespace std;

[ACM] hdu 1242 【BFS】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242 参考链接:http://www.acmerblog.com/hdu-1242-Rescue-1605.html 普通队列+bfs确实是蒙对的,因为击败守卫需要消耗时间1,因此普通队列每一次出队列的元素只是步数上最优,但不一定是时间上最优的,这时即使我们把整个迷宫搜完以最小值为最优依然不行,因为每一步处理完成都会把该状态标记为已处理vis[i][j]=1,因此,只要有一步不是最优,就会影响后面的

[ACM] HDU 1242 Rescue (优先队列)

Rescue 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:

HDU 1242 (BFS+优先队列)

题意:X代表卫兵,a代表终点,r代表起始点,.代表路,#代表墙,走过.要花费一秒,走过x要花费2秒,求从起点到终点的最少时间. 析:一看到样例就知道是BFS了吧,很明显是最短路径问题,不过又加了一个条件——时间,所以我们用优先队列去优先获取时间短的路径,总体实现起来没有太大难度. 代码如下: #include <iostream> #include <cstdio> #include <vector> #include <set> #include <

HDU 1242 rescue and 优先队列入门

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: approa

HDU 1242 Rescue(BFS)

题目链接 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: a

HDU 1242 Rescue(优先队列+bfs)

题目地址:HDU 1242 这个题相比于普通的bfs有个特殊的地方,经过士兵时会额外消耗时间,也就是说此时最先搜到的时候不一定是用时最短的了.需要全部搜一遍才可以.这时候优先队列的好处就显现出来了.利用优先队列,可以让队列中的元素按时间排序,让先出来的总是时间短的,这样的话,最先搜到的一定是时间短的,就不用全部搜一遍了.PS:我是为了学优先队列做的这题..不是为了这题而现学的优先队列.. 代码如下: #include <iostream> #include <stdio.h> #i