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 <iostream>
#include <string.h>
#include <cstdio>
#include <queue>
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_queue<node>q;
    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<n&&next.y>=0&&next.y<m&&a[next.x][next.y]!='#')
            {
                if(a[next.x][next.y]=='a')
                {
                    printf("%d\n",next.time);
                    return;
                }
                if(a[next.x][next.y]=='x')
                {
                    next.time++;
                    a[next.x][next.y]='#';
                    q.push(next);
                }
                else
                {
                    a[next.x][next.y]='#';
                    q.push(next);
                }
            }
        }
    }
    cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
}
int main()
{
    int i,j;
    while (scanf ("%d %d",&n,&m)!=EOF)
    {
        memset(a,'\0',sizeof(a));
        for (i=0; i<n; i++)
        {
            for (j=0; j<m; j++)
            {
                cin>>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;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值