HDU 1242 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.) 

InputFirst 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. 
OutputFor 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
题解:
正常的最短路径搜索题,需要注意的是遇到士兵的时候会多停顿一秒,要用优先队列,步数少的优先级较高。
代码:
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=205;
char maze[maxn][maxn];
bool vis[maxn][maxn];
struct node
{
    int x,y,step;
    bool operator < (const node &a) const//重载"<",步数少的优先级高
    {
        return step > a.step;
    }
};
int n,m,sx,sy,gx,gy;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int bfs()
{
    priority_queue<node> que;
    node cur;
    cur.step=0;
    cur.x=sx;
    cur.y=sy;
    que.push(cur);
    while(que.size())
    {
        cur=que.top();//优先队列取出第一个元素用top
        que.pop();
        if(cur.x==gx&&cur.y==gy)
            return cur.step;
        for(int i=0;i<4;i++)
        {
            node next;
            next.x=cur.x+dx[i],next.y=cur.y+dy[i];
            if(0<=next.x&&next.x<n&&0<=next.y&&next.y<m&&maze[next.x][next.y]!='#'&&!vis[next.x][next.y])
            {
                if(maze[next.x][next.y]=='x')
                    next.step=cur.step+2;
                else
                    next.step=cur.step+1;
                que.push(next);
                vis[next.x][next.y]=true;
            }
        }
    }
    return -1;
}
int main()
{
    while(cin>>n>>m)
    {
        memset(vis,false,sizeof(vis));
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                {
                    cin>>maze[i][j];
                    if(maze[i][j]=='a')
                    {
                        gx=i;
                        gy=j;
                    }
                    if(maze[i][j]=='r')
                    {
                        sx=i;
                        sy=j;
                    }
                }
        int ans=bfs();
        if(ans==-1)
            cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
        else
            cout<<ans<<endl;
    }
    return 0;
}

 

 

转载于:https://www.cnblogs.com/orion7/p/7299253.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值