HDU-1242-Rescue(BFS+优先队列)

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

Sample Output
13

[分析]
其实一开始就是有思路的,觉得要BFS,但是守卫要杀要多加一步,这个部分如果只用BFS特别难写。然后就任性的用了DFS,果然TLE了。然后改BFS,发现BFS写是有问题的,最后才改成了BFS+优先队列。(真香..)

好了,正题。
首先BFS求最短是毋庸置疑了,问题就是优先队列。以时间为排序标准,把时间少的放前面。其实和原本的队列是差不多的,主要是加了守卫不好写,因为守卫要先杀,再走到那一格里面。所以这里加了优先队列就不用考虑后面有没有某个Node乱了顺序。

感觉讲的不太好,反正归根结底就是平常走1步,但是有守卫的格子要2步,所以把这个节点放到队列后面某位置。

[代码]

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
char a[205][205];
bool vis[205][205];
int dire[4][2] = { {1,0},{0,1},{-1,0},{0,-1} };
int rx, ry;
int ans;

struct Node
{
    int x, y;
    int cnt;
    friend bool operator<(Node a, Node b)
    {
        return a.cnt>b.cnt;
    }
}sc,now;

void findR(int n,int m)
{
    for (int i = 1; i < n; i++)
    {
        for (int j = 1; j < m; j++)
        {
            if (a[i][j] == 'r')
            {
                rx = i;
                ry = j;
                return;
            }
        }
    }
}

void bfs()
{
    priority_queue<Node>q;

    sc.x = rx;
    sc.y = ry;
    sc.cnt = 0;
    q.push(sc);
    while (q.size())
    {
        now = q.top();
        q.pop();
        if (a[now.x][now.y] == 'a')
        {
            ans = now.cnt;
            return;
        }
        for (int i = 0; i < 4; i++)
        {
            int nowx = now.x + dire[i][0], nowy = now.y + dire[i][1];
            if (!vis[nowx][nowy] && a[nowx][nowy] != '#')
            {
                vis[nowx][nowy] = 1;
                sc.x = nowx;
                sc.y=nowy;
                if (a[nowx][nowy] == 'x')sc.cnt = now.cnt + 2;
                else sc.cnt = now.cnt + 1;
                q.push(sc);
            }
        }
    }
}

int main()
{
    int n, m;
    while (cin >> n >> m)
    {
        memset(a, '#', sizeof(a));
        memset(vis, 0, sizeof(vis));
        for (int i = 1; i <= n; i++)
        {
            scanf("%s", a[i]+1);
        }
        for (int i = 1; i <= n; i++)
        {
            a[i][m+1] = '#';
        }
        findR(n, m);
        ans = INF;
        bfs();
        if(ans!=INF)printf("%d\n", ans);
        else printf("Poor ANGEL has to stay in the prison all his life.\n");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值