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

H - Rescue
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

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

广度搜索加优先队列,很常规的一道题
尝试了一下重载操作符
代码

#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
//又是广度搜索,优先队列
char map[202][202];//地图数据
int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}}; //搜索方向
int visited[202][202];//已搜索标记为1,未搜索标记为0
int m,n;//地图大小
int starx,stary;//起始点坐标
struct node
{
    int x;
    int y;
    int time;
    friend bool operator < (const node &a,const node &b)//operator操作符重载
    {
        return a.time>b.time;//耗时少的排在队伍,先出列
    }
};
void BFS()
{
    priority_queueq;//优先队列
    node star,end;
    star.x=starx;//记录起始点坐标
    star.y=stary;
    star.time=0;//初始化时间
    memset(visited,0,sizeof(visited));//初始化为未访问
    visited[star.x][star.y]=1;//起始点标记为已访问
    q.push(star);
    while(!q.empty())//优先队列
    {
        star=q.top();
        q.pop();
        if(map[star.x][star.y]=='r')//如果到达终点
        {
            printf("%d\n",star.time);
            return;
        }
        for(int i=0; i<4; i++)
        {
            end.x=star.x+dir[i][0];
            end.y=star.y+dir[i][1];
            if(end.x>=0&&end.x=0&&end.ymap[end.x][end.y]!='#'&&visited[end.x][end.y]==0)//如果此结点合法
            {
                visited[end.x][end.y]=1;//标记此合法坐标为已访问
                if(map[end.x][end.y]=='x')//如果遇到特殊点
                    end.time=star.time+2;
                else
                    end.time=star.time+1;
                q.push(end);
            }
        }
    }
    printf("Poor ANGEL has to stay in the prison all his life.\n");//还有个句号要输出
}
int main()
{
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        for(int i=0; ifor(int j=0; jcin>>map[i][j];
                if(map[i][j]=='a')
                {
                    starx=i;
                    stary=j;
                    map[i][j]='#';
                }
            }
        }
        BFS();
    }
}

你可能感兴趣的:(搜索)