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

思路:

根据题意是找最短路径问题,那就想到用BFS做,正常的BFS用队列就可以了,但是这题特殊在遇到敌人时可以走,但是时间需要增加,也就是说到每一个格子所需的时间不一定是简单的广度搜索扩展下去的,所以这题还要用到优先队列,每次放入队列时,自动地把时间小的放在队列前面,先拿出来进行扩展搜索,这样就可以保证找到Angle时的时间是最小的了。

注意点:

  • 另外需要注意的是,优先队列默认是把大的放前面先出队的,所以重载小于运算符的时候要反过来,实际是返回大于运算的结果。
  • 还有重载时,如果是在结构体里面定义的成员函数,那么重载函数要定义成const,否则会报错,具体的涉及内部原理,我也没弄太明白。如果是定义成友元函数,则不必定义成const。两种重载方式见下。

bool operator<(const node &n) const {

        return step > n.step;

}

friend bool operator<(const node&n1, const node &n2) {

         return n1.step > n2.step;

}

  • 这题的样例虽然只有一个r,但是根据题意可能有多个r,只要一个 找到angle就可以,所以要多次BFS找到最小的时间。还有一点,根据这题的Discuss区,貌似还有Angle不在监狱中的样例。。。所以要注意找不到的时候判断一下。

下面是AC代码:

#include<iostream>
#include<fstream>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;

struct node{
    int x, y;
    char c;
    int step;
    node() {}
    node(int x, int y):x(x), y(y),step(0) {}
    bool operator<(const node &n) const{
        return step > n.step;
    }
};

int n, m;
node maze[205][205];
bool vis[205][205];
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int minn;

bool judge(int x, int y);
int bfs(int x, int y);

int main(void)
{
    // ifstream fin("in.txt");
    // cin.rdbuf(fin.rdbuf());

    while(cin >> n >> m && n)
    {  
        memset(maze, 0, sizeof(maze));   //每一个样例开始都要记得把所有变量重新初始化一下,以防万一
        minn = 0x3f3f3f3f;  
        vector<node> rv;   //存放r,不知道有多少个,就用vector好了

        for(int i = 0; i < n; ++i)
        {
            for(int j = 0 ; j < m; ++j){
                cin >> maze[i][j].c;
                maze[i][j].x = i;
                maze[i][j].y = j;
                if(maze[i][j].c == 'r'){
                    rv.push_back(node(i, j));
                }
            }
        }

        for(vector<node>::iterator it = rv.begin(); it != rv.end(); ++it)   //对每一个r进行BFS,找到最小的时间
        {
            memset(vis, false, sizeof(vis));
            maze[it->x][it->y].step = 0;      //每一个r开始时step要记得初始化为0
            int temp = bfs(it->x, it->y);
            if(minn > temp){
                minn = temp;
            }
        }

        if(minn != 0x3f3f3f3f)
            cout << minn << endl;
        else
            cout << "Poor ANGEL has to stay in the prison all his life." << endl;    
    }

    return 0;
}

bool judge(int x, int y)
{
    if(x < 0 || x >= n || y < 0 || y >= m || vis[x][y] || maze[x][y].c == '#')
        return false;
    return true;
}

int bfs(int x, int y)
{
    vis[x][y] = 1;
    priority_queue<node> pq;
    pq.push(maze[x][y]);

    while(!pq.empty())
    {
        node tmp = pq.top();
        pq.pop();
        
        if(maze[tmp.x][tmp.y].c == 'a'){
            return tmp.step;
        }

        for(int i = 0; i < 4; ++i)
        {
            int xx = tmp.x + dir[i][0];
            int yy = tmp.y + dir[i][1];

            if(judge(xx, yy))
            {
                vis[xx][yy] = 1;

                if(maze[xx][yy].c == 'x')
                {
                    maze[xx][yy].step = tmp.step + 2;
                    pq.push(maze[xx][yy]); 
                }
                else
                {
                    maze[xx][yy].step = tmp.step+1;
                    pq.push(maze[xx][yy]);
                }   
            }
        }
    }
    return 0x3f3f3f3f;    //没找到的情况。
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值