hdu 1242 Rescue(优先队列)

来源:互联网 发布:淘宝买家怎么导出订单 编辑:程序博客网 时间:2024/05/21 17:46

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

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
题目大意:'a'代表公主的位置,'r'代表救公主的朋友的位置,'x'代表守卫的位置,每走一步需要1时间,杀死一个守卫也需要1时间,'#'是墙,可能有多个朋友,求救出公主的最短时间,如果没有路径可以到达公主,输出"Poor ANGEL has to stay in the prison all his life.";

#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#include <stack>#include <queue>using namespace std;#define N 310#define met(a, b) memset (a, b, sizeof (a))typedef long long LL;//const int INF = ((1<<31)-1);const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};char str[N][N];int k, n, m, vis[N][N];struct node{    int x, y, t;    friend bool operator < (const node &a, const node &b)    {        return a.t > b.t;//时间短的先出队    }}sa;int BFS (){    priority_queue <node> que;//优先队列    que.push (sa);    met (vis, 0);    vis[sa.x][sa.y] = 1;    while (que.size())    {        node Sa = que.top(); que.pop();        if (str[Sa.x][Sa.y] == 'r') return Sa.t;        for (int i=0; i<4; i++)        {            node q = Sa;            q.x += dir[i][0], q.y += dir[i][1];            if (q.x>=0 && q.x<n && q.y>=0 && q.y<m && !vis[q.x][q.y] && str[q.x][q.y]!='#')            {                q.t++;                vis[q.x][q.y] = 1;                if (str[q.x][q.y] == 'x')                    q.t++;                que.push (q);            }        }    }    return -1;}int main (){    while (scanf ("%d %d", &n, &m) != EOF)    {        k = 0;        for (int i=0; i<n; i++)        {            getchar ();            for (int j=0; j<m; j++)            {                scanf ("%c", &str[i][j]);                if (str[i][j] == 'a')                sa = (node) {i, j, 0};            }        }        int ans = BFS ();        if (ans == -1) puts ("Poor ANGEL has to stay in the prison all his life.");        else printf ("%d\n", ans);    }    return 0;}


0 0
原创粉丝点击