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

解题思路:

简单的优先队列题目。复习一下优先队列用法.

代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <cmath>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cctype>
using namespace std;
const int maxn=202;
char mp[maxn][maxn];
bool vis[maxn][maxn];
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};
int sx,sy,ex,ey;
int n,m;

struct Node
{
    int x,y;
    int step;
};
int step[maxn][maxn];

bool operator<(Node a,Node b)
{
    return a.step>b.step;
}

bool ok(int x,int y)
{
    if(x>=1&&x<=n&&y>=1&&y<=m&&mp[x][y]!='#')
        return true;
    return false;
}

void BFS(int x,int y)
{
    step[x][y]=0;
    memset(vis,0,sizeof(vis));
    vis[x][y]=1;
    priority_queue<Node>q;
    Node tp;
    tp.x=x,tp.y=y,tp.step=0;
    q.push(tp);
    while(!q.empty())
    {
        Node first=q.top();
        if(first.x==ex&&first.y==ey)
            break;
        q.pop();
        for(int i=0;i<4;i++)
        {
            int nextx=first.x+dx[i];
            int nexty=first.y+dy[i];
            if(!vis[nextx][nexty]&&ok(nextx,nexty))
            {
                vis[nextx][nexty]=1;
                tp.x=nextx;
                tp.y=nexty;
                if(mp[nextx][nexty]=='x')
                {
                    step[nextx][nexty]=first.step+2;
                    tp.step=first.step+2;
                }
                else
                {
                    step[nextx][nexty]=first.step+1;
                    tp.step=first.step+1;
                }
                q.push(tp);
            }
        }
    }
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        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;
            else if(mp[i][j]=='a')
                ex=i,ey=j;
        }
        BFS(sx,sy);
        if(!vis[ex][ey])
            printf("Poor ANGEL has to stay in the prison all his life.\n");
        else
            printf("%d\n",step[ex][ey]);
    }
    return 0;
}

优先队列用法:

///优先队列是默认int从大到小priority_queue<int>q1,也可以定义为从小到大priority_queue<int,vector<int>,greater<int> >q2;
也可以自定义优先级,重载<

#include <iostream>
#include <functional>
#include <queue>
using namespace std;

///自定义优先级,两种写法,按照优先级从大到小的顺序
/*
struct node
{
    friend bool operator<(node n1,node n2)
    {
        return n1.priority<n2.priority;
    }
    int priority;
    int value;
};*/

struct node
{
    int priority;
    int value;
};
bool operator<(node a,node b)
{
    return a.priority<b.priority;
}

int main()
{
    const int len=5;
    int i;
    int a[len]={3,5,9,6,2};
    //优先队列中从大到小输出
    priority_queue<int>q1;
    for(i=0;i<len;i++)
        q1.push(a[i]);
    for(int i=0;i<len;i++)
    {
        cout<<q1.top();
        q1.pop();
    }
    cout<<endl;
    //优先队列中从小到大输出

    /**
    priority_queue<int,vector<int>,greater<int> >q2;
    for(i=0;i<len;i++)
        q2.push(a[i]);
    for(i=0;i<len;i++)
    {
        cout<<q2.top();
        q2.pop();
    }*/

    priority_queue<int,vector<int>,greater<int> >q;
    for(int i=0;i<len;i++)
        q.push(a[i]);
    while(!q.empty())
    {
        cout<<q.top();
        q.pop();
    }
    cout<<endl;
    //按照某个优先级输出,该代码中为priority值大的先输出
    priority_queue<node>q3;
    node b[len];
    b[0].priority=6;b[0].value=1;
    b[1].priority=9;b[1].value=5;
    b[2].priority=2;b[2].value=3;
    b[3].priority=8;b[3].value=2;
    b[4].priority=1;b[4].value=4;
    for(i=0;i<len;i++)
        q3.push(b[i]);
    cout<<"优先级"<<'\t'<<"值"<<endl;
    for(i=0;i<len;i++)
    {
        cout<<q3.top().priority<<'\t'<<q3.top().value<<endl;
        q3.pop();
    }
    return 0;
}

运行:

96532

23569

优先级  值

9       5

8       2

6       1

2       3

1       4

时间: 2024-10-20 17:47:18

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

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

HDU 1242 Rescue(优先队列+bfs)

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

hdu - 1242 Rescue (优先队列+bfs)

http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了. 但是别人说要用优先队列来保证时间最优,我倒是没明白,步数最优跟时间最优不是等价的吗?就算士兵要花费额外时间,可是既然先到了目标点那时间不也一定是最小的? 当然用优先队列+ a去搜索r是最稳妥的. 1 #include <cstdio> 2 #include <cstring> 3

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

Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 14   Accepted Submission(s) : 7 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Angel was caught by the MOLIGPY

HDU 1242——Rescue(优先队列)

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

HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 开始以为是水题,想敲一下练手的,后来发现并不是一个简单的搜索题,BFS做肯定出事...后来发现题目里面也有坑 题意是从r到a的最短距离,"."相当时间单位1,"x"相当时间单位2,求最短时间 HDU 搜索课件上说,这题和HDU1010相似,刚开始并没有觉得像剪枝,就改用  双向BFS   0ms  一Y,爽! 网上查了一下,神牛们竟然用BFS+

ZOJ 1649 &amp;&amp; HDU 1242 Rescue

Rescue Time Limit: 2 Seconds      Memory Limit: 65536 KB 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

HDU 1242 Rescue营救 BFS算法

题目链接:HDU 1242 Rescue营救 Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16524    Accepted Submission(s): 5997 Problem Description Angel was caught by the MOLIGPY! He was put in prison by

hdu 1242 Rescue (BFS+优先队列)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1242 这道题目我是用BFS+优先队列做的.听说只用bfs会超时. 因为这道题有多个营救者,所以我们从被营救者开始bfs,找到最近的营救者就是最短时间. 先定义一个结构体,存放坐标x和y,还有到达当前点(x,y)消耗的时间. struct node { int x,y; int time; friend bool operator < (const node &a,const node &