HDU1242 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 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” 是天使,“b”是天使的朋友, “x”是守卫,“#”是墙,“.”是通路。天使的朋友通过道路耗时为1,通过守卫耗时为2,寻求最快到达天使位置的耗时,如果不能到达,则输出”Poor ANGEL has to stay in the prison all his life.”。

解析

迷宫类最短问题,采用BFS策略,该题要求耗时最短,因此需要采用优先队列对将要走的路径进行优先级排序,时间较小的优先级高。

#include<iostream>
#include<math.h>
#include <algorithm>
#include<stdio.h>
#include<string.h>
#include<stack> 
#include<queue>
using namespace std;
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define req(i, a, b) for(int i=(a); i<=(b); i++)
#define ull unsigned __int64
#define sc(t) scanf("%d",&(t))
#define sc2(t,x) scanf("%d%d",&(t),&(x))
#define pr(t) printf("%d\n",(t))
#define pf printf
#define prk printf("\n")
#define pi acos(-1.0)
#define ms(a,b) memset((a),(b),sizeof((a)))
#define mc(a,b) memcpy((a),(b),sizeof((a))) 
#define w while
#define vr vector<int>
#define gr greater<int>
typedef long long ll;


int n, m;
char s[205][205];
bool f[205][205];
int dx[4] = {-1, 0, 0, 1};
int dy[4] = { 0, 1,-1, 0};

class acm{
public:
    int x;
    int y;
    int time;
friend bool operator <(acm t1, acm t2)
{
    return t1.time > t2.time; //重载"<",按给定优先级排序
}
};


bool bfs(int x, int y)
{
    priority_queue<acm> q;
    f[x][y] = 1;
    acm fi, se, now;
    fi.x = x;
    fi.y = y;
    fi.time = 0;
    q.push(fi);
    w(!q.empty())
    {
        se =q.top();
        q.pop();
        if(s[se.x][se.y] == 'a')
        {
            pr(se.time);
            return true;
        } 
        rep(i, 0, 4)
        {
            now.x = se.x + dx[i];
            now.y = se.y + dy[i];
            if(now.x >= 1 && now.x <= n && now.y >= 1 && now.y <= m && s[now.x][now.y] != '#' && !f[now.x][now.y]) //又把f给忘记判断了 
            {
                f[now.x][now.y] = 1;
                if(s[now.x][now.y] == '.' || s[now.x][now.y] == 'a')
                now.time = se.time + 1;
                else if(s[now.x][now.y] == 'x')
                now.time = se.time + 2;
                q.push(now);
            }
        }
    }
    return false;
}

int main()
{
    int x, y;
    w(~sc2(n, m))
    {
        getchar();
        req(i, 1, n)
        {
            req(j, 1, m)
            {
                scanf("%c",&s[i][j]);
                if(s[i][j] == 'r')
                x = i, y = j;
            }
            getchar(); //缓冲换行符
        }
        ms(f, 0);
        if(!bfs(x, y))
        pf("Poor ANGEL has to stay in the prison all his life.\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值