HDU 1242 Rescue 优先队列 BFS

原题: http://acm.hdu.edu.cn/showproblem.php?pid=1242

原题:

Rescue

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21698 Accepted Submission(s): 7743

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

思路:

从a走到r,‘#’是墙,‘.’是用时为1的路,‘x’是用时为2的路,要求用时最少。
只需要传统的bfs搜图加上优先队列即可。

代码:

#include 
#include"string.h"
#include"cstdio"
#include"stdlib.h"
#include"algorithm"
#include"queue"
#include"functional"

using namespace std;
typedef long long int lint;
int mov[4][2]= {-1,0,1,0,0,1,0,-1};
typedef struct point
{
    friend bool operator< (point p1,point p2)
    {
        return p1.dis>p2.dis;
    }
    int x;
    int y;
    int dis;
    int flag;
    char tu;
} point;
const int bc=205;
point p[bc][bc];


int bfs(int sx,int sy,int m,int n)
{
    priority_queue  q;
    q.push(p[sx][sy]);
    while(!q.empty())
    {
        point tem=q.top();
        q.pop();

        for(int i=0; i<4; i++)
        {
            int tx=tem.x+mov[i][0];
            int ty=tem.y+mov[i][1];

            if(tx>0&&tx<=m&&ty>0&&ty<=n&&(p[tx][ty].flag)==0&&(p[tx][ty].tu!='#'))
            {
                if(p[tx][ty].tu=='.')   p[tx][ty].dis=tem.dis+1;
                if(p[tx][ty].tu=='x')   p[tx][ty].dis=tem.dis+2;
                if(p[tx][ty].tu=='r')
                {
                    return tem.dis+1;
                }
                p[tx][ty].flag=1;
                q.push(p[tx][ty]);
            }
        }
    }
    return 0;
}

int main()
{
    int m,n;
    int sx,sy;
    int ex,ey;
    while(scanf("%d %d",&m,&n)!=EOF)
    {
        getchar();
        for(int i=1; i<=m; i++)
        {
            for(int j=1; j<=n; j++)
            {
                p[i][j].x=i;
                p[i][j].y=j;
                p[i][j].dis=0;
                p[i][j].flag=0;
                scanf("%c",&p[i][j].tu);
                if(p[i][j].tu=='a')
                {
                    sx=i;
                    sy=j;
                }
                if(p[i][j].tu=='r')
                {
                    ex=i;
                    ey=j;
                }
            }
            getchar();
        }
        int an=bfs(sx,sy,m,n);
        if(an)  printf("%d\n",an);
        else printf("Poor ANGEL has to stay in the prison all his life.\n" );
    }

}

你可能感兴趣的:(bfs)