HDU 1242 Rescue(优先队列+广度优先搜索)

Rescue

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

基本题意:
#代表墙,不能通过,x代表守卫,耗时2秒,. 代表路耗时1秒。r代表angel的朋友,a代表angel;输出r找到angel的最短时间。
不使用优先队列,可能输出的时间不是最短时间
本题也可以从angel的位置开始搜素,直到找到r,就输出时间


//代码
#include <stdio.h>
#include <math.h>
#include <queue>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=250;
int w,h;
int xs,ys;  //起始位置
int flag;   //是否找到angel
char moli[N][N];
int v[N][N];
int d[][2]= {{0,1},{-1,0},{0,-1},{1,0}};        //方向
struct P
{
    int x,y,step;
} ;
/*优先队列排序,排序符号与正常情况相反,从小到大排序,要用小于号,从大到小排序要用大于号*/
struct cmp
{
    bool operator () (P a,P b)
    {
        return a.step>b.step;
    }
};
int judge(int x,int y)
{
    if( x<0 || y<0 || x>h || y>w )
        return 0;
    if(v[x][y])
        return 0;
    if( moli[x][y]=='#' )
        return 0;
    return 1;
}
/*广度优先搜素是利用队列实现*/
void BFS()
{
    P a,b;
    flag=0;
    memset(v,0,sizeof v);
    v[xs][ys]=1;
    a.x=xs;
    a.y=ys;
    a.step=0;
    /*优先队列定义*/
    priority_queue<P,vector<P>,cmp >Q;
    Q.push(a);
    while (!Q.empty())
    {
        a=Q.top();
        Q.pop();
        /*找到angel输出时间*/
        if (moli[a.x][a.y]=='a')
        {
            printf("%d\n",a.step);
            flag=1;
            break;
        }
        /*判断一个格子的上,下,左,右是否可走*/
        for (int i=0; i<4; i++)
        {
            b.x=a.x+d[i][0];
            b.y=a.y+d[i][1];
            if (judge(b.x,b.y))
            {
                if (moli[b.x][b.y]=='x')
                    b.step=a.step+2;
                else
                    b.step=a.step+1;
                v[b.x][b.y]=1;
                Q.push(b);
            }
        }
    }
}
int main()
{
    while (~scanf("%d %d",&h,&w))
    {
        for (int i=0; i<h; i++)
        {
            for (int j=0; j<w; j++)
            {
                scanf("%c",&moli[i][j]);
                if (moli[i][j]=='r')
                    xs=i,ys=j;
            }
            getchar();
        }
        BFS();
        if (!flag)
            printf("Poor ANGEL has to stay in the prison all his life.\n");
    }
    return 0;
}
/*第二种优先队列调用方式*/
/*当入队元素为结构体时,可以把"自定义的优先级"定义在结构体内*/
//struct P
//{
//    int x,y,step;
//    friend bool operator < (P a,P b)
//    {
//        return a.step>b.step;
//    }
//} ;
/*优先队列调用方式*/
//priority_queue<P>Q;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值