hdu-1242 Rescue

Rescue


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

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
翻译:
天使被MOLIGPY抓住了! 他被Moligpy囚禁在监狱里。 监狱被描述为N * M(N,M <= 200)矩阵。 监狱里有墙,道路交通工具和警卫。
天使的朋友想拯救天使。 他们的任务是:接近天使。 我们假设“天使”是要到达天使所在的位置。 当在网格中有一个守卫时,我们必须杀死他(或她?)进入网格。 我们假设我们向上,向下,向右,向左,我们需要1个单位时间,杀死一个警卫也需要1个单位时间。 我们足够强大,可以杀死所有的守卫。
你必须计算接近天使的最短时间。 (当然,我们只能将UP,DOWN,LEFT和RIGHT移动到边界内的邻居网格。)
输入:
第一行包含两个整数代表N和M.然后N行,每行都有M个字符。“” 代表道路,“a”代表天使,“r”代表天使的每个朋友。 进程到文件的末尾。
输出:
对于每个测试用例,您的程序应输出一个整数,代表所需的最短时间。 如果这样的数字不存在,你应该输出一行包含“可恶的天使必须留在监狱里的一生”。
 
    
解题思路:
简单广搜+优先队列
更正 :
再次写 这道题 没有用优先队列也过了 所以 其实 就是简单广搜

#include 
#include 
#include 
#include 
using namespace std;
char a[200][200];
int cx[4][2]= {0,1,0,-1,1,0,-1,0};
int n,m;
struct node
{
    int x;
    int y;
    int step;
    int time;
    friend bool operator < (node c,node d)
    {
        return c.time > d.time;
    }
} now;
void bfs()
{
    int i;
    priority_queueq;
    node next;
    q.push(now);
    while (!q.empty())
    {
        now = q.top();
        q.pop();
        if(a[now.x][now.y] == 'a')
        {
            printf("%d\n",now.time);
            return;
        }
        for (i=0; i<4; i++)
        {
            next.x = now.x+cx[i][0];
            next.y = now.y+cx[i][1];
            next.time = now.time + 1;
            if (next.x>=0&&next.x=0&&next.y>a[i][j];
                if (a[i][j]=='r')
                {
                    now.x = i;
                    now.y = j;
                    now.step = 0;
                    now.time = 0;
                    a[i][j]='#';
                }
            }
        }
        bfs();
    }
    return 0;
}


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