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

这道题是刚刚学完搜索和优先队列做的,连找bug做了一天,不过最后终于整明白了,突然感觉算法好神奇。
思路:
Angel去找朋友和朋友去找她的路线是一样的,因为朋友可能有多个,所以我们可以根据Angel找朋友确定最短时间。

AC代码:

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<string.h>
using namespace std;
char aa[250][250];
int f[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int book[250][250];
int n,m;
struct lyf
{
    int x,y,time;
    friend bool operator<(lyf a,lyf b)//友元函数,运算符重载,最小时间优先。
    {
        return a.time>b.time;
    }
};
int bfs(int ax,int ay)
{
    //printf("%d %d\n",m,n);
    lyf yy,ff;
    priority_queue<lyf>q;//优先队列
    while(!q.empty()) q.pop();
    yy.x=ax;
    yy.y=ay;
    yy.time=0;
    q.push(yy);//将Angel的位置,和该点的时间进入队列
    int i;
    while(!q.empty())

        yy=q.top();//每次都要取队首作为被搜索元素, 因为队首是最短时间
//(即使非队首元素在本次搜索时,可能已经找到朋友,但是,我们还是要搜索一下队首能不能在比该非队首元素用时最少的情况下找到朋友)——————这关键点一开始理解起来很费劲,多思考一下就能明白了
        q.pop();
        if(aa[yy.x][yy.y]=='r') return yy.time;//如果找到朋友,返回此时用时(跟第几次被搜索到没有关系,我们只取找到朋友的最短时间)
        for(i=0;i<4;i++)
    {
        ff.x=yy.x+f[i][0];
        ff.y=yy.y+f[i][1];
        if(ff.x>=0&&ff.x<n&&ff.y>=0&&ff.y<m&&book[ff.x][ff.y]==0&&aa[ff.x][ff.y]!='#')//四个方向可以继续搜索的条件
        {
            if(aa[ff.x][ff.y]=='x')//遇到士兵消耗两个时间
            {ff.time=yy.time+2;}
            else
            {
                ff.time=yy.time+1;//否则消耗一个时间
            }
            book[ff.x][ff.y]=1;//标记该点已经被搜索过
            q.push(ff);//将该点进入队列
        }
    }
    }
    return -1;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        getchar();
        int i,j,lx,ly,hh;
        for(i=0;i<n;i++)
        {
           scanf("%s",&aa[i]);
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(aa[i][j]=='a')
                {
                     lx=i;
                     ly=j;
                    break;
                }
            }
        }
        memset(book,0,sizeof(book));
        book[lx][ly]=1;
        hh=bfs(lx,ly);
        //printf("%d\n",hh);
        if(hh==-1) printf("Poor ANGEL has to stay in the prison all his life.\n");
        else printf("%d\n",hh);
    }
}

题目传送门:Rescue(点击可交题)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值