zoj 1649 Rescue【BFS+优先队列】

本文介绍了一个救援任务问题,其中需要计算从起始位置到达目标位置所需的最短时间,地图上包含障碍物、守卫等元素。文章详细解释了使用优先级队列的BFS算法来求解这一问题的方法。

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 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要救r,x是警察,a走到“.”的耗时为1,a要想干掉"x"耗时也是1,因为走到这个格子耗时也是 1,所以加在一起就是2,。问最小救到r的步数,如果不能救到,输出Poor ANGEL has to stay in the prison all his life.


思路:因为有耗时不同的差别,所以直接用BFS队列实现出来的结果不是最优的,所以呢,我们要控制一个时间最优的问题,所以我们采用时间优先队列来解决这个问题。。


AC代码:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;

struct zuobiao
{
    int x,y,output;
    friend bool operator <(zuobiao a,zuobiao b)
    {
        return a.output>b.output;
    }
}now,nex;
int n,m;
char a[1000][1000];
int vis[1000][1000];
int fx[4]={1,-1,0,0};
int fy[4]={0,0,1,-1};
void bfs(int x,int y)
{
    priority_queue< zuobiao >s;
    memset(vis,0,sizeof(vis));
    now.x=x;
    now.y=y;
    now.output=0;
    vis[now.x][now.y]=1;
    s.push(now);
    while(!s.empty())
    {
        now=s.top();
        if(a[now.x][now.y]=='r')
        {
            printf("%d\n",now.output);
            return ;
        }
        s.pop();
        for(int i=0;i<4;i++)
        {
            nex.x=now.x+fx[i];
            nex.y=now.y+fy[i];
            nex.output=now.output+1;
            if(nex.x>=0&&nex.x<n&nex.y>=0&nex.y<m&&a[nex.x][nex.y]!='#'&&vis[nex.x][nex.y]==0)
            {
                vis[nex.x][nex.y]=1;
                if(a[nex.x][nex.y]=='x')
                {
                    nex.output++;
                    s.push(nex);
                }
                else s.push(nex);
            }
        }
    }
    printf("Poor ANGEL has to stay in the prison all his life.\n");
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int sx,sy;
        for(int i=0;i<n;i++)
        {
            scanf("%s",&a[i]);
            for(int j=0;j<n;j++)
            {
                if(a[i][j]=='a')
                {
                    sx=i;sy=j;
                }
            }
        }
        bfs(sx,sy);
    }
}









这是一个使用优先队列(priority queue)的题目,可以使用C++ STL中的优先队列来实现。 以下是样例代码: ```c++ #include <iostream> #include <queue> // 包含 priority_queue 头文件 using namespace std; int main() { int n; while (cin >> n) { priority_queue<int, vector<int>, greater<int>> pq; // 定义小根堆 for (int i = 0; i < n; i++) { int x; cin >> x; pq.push(x); // 将元素加入优先队列 } int ans = 0; while (pq.size() > 1) { // 只要队列中还有两个及以上元素 int a = pq.top(); pq.pop(); int b = pq.top(); pq.pop(); ans += a + b; pq.push(a + b); // 新元素入队 } cout << ans << endl; } return 0; } ``` 在这个代码中,我们使用了C++ STL中的优先队列 `priority_queue`,它有三个模板参数: - `int`:表示队列中存储的元素类型是 `int`。 - `vector<int>`:表示队列内部使用 `vector` 作为基础容器。 - `greater<int>`:表示使用小根堆来存储元素。 在主函数中,我们首先读入元素,然后将它们加入到优先队列中。接着,我们依次取出队列中的两个最小元素,将它们相加并累加到答案中,然后将它们的和作为新元素加入到队列中。最后,当队列中只剩下一个元素时,输出答案。 需要注意的是,在使用小根堆时,我们要将模板参数 `greater<int>` 作为第三个参数传递给 `priority_queue`。如果不指定第三个参数,则默认使用大根堆。 另外,这里使用了 `while (cin >> n)` 的方式来不断读入测试用例,直到遇到输入结束符为止(比如EOF或者Ctrl+Z)。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值