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

题解:广搜,每次找时间最短的一个,因为遇到士兵时间多1,此时这个点应该排在后面。所以用优先队列排序。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

struct Node
{
    int x;
    int y;
    int time;
    Node(int a,int b,int c)
    {
        x = a;
        y = b;
        time = c;
    }
    bool operator< (Node t) const
    {
        return time > t.time;
    }
};

int sx,sy;
int n,m;
int ans;
char map[205][205];
int d[4][2] = {{0,-1},{0,1},{-1,0},{1,0}};

bool bfs()
{
    priority_queue<Node> q;
    q.push(Node(sx,sy,0));
    ans = 0;
    map[sx][sy] = ‘#‘;
    while(!q.empty())
    {
        Node p = q.top();
        q.pop();
        for(int i = 0;i < 4;i++)
        {
            int x = p.x + d[i][0];
            int y = p.y + d[i][1];
            if(x >= 0 && x <  n && y >= 0 && y <  m && map[x][y] != ‘#‘)
            {
                if(map[x][y] == ‘a‘)
                {
                    ans = p.time + 1;
                    return true;
                }
                if(map[x][y] == ‘x‘)
                {
                    q.push(Node(x,y,p.time + 2));
                }
                else
                {
                    q.push(Node(x,y,p.time + 1));
                }
                map[x][y] = ‘#‘;
            }
        }
    }

    return false;

}

int main()
{
    while(scanf("%d%d",&n,&m) != EOF)
    {
        getchar();
        for(int i = 0;i < n;i++)
        {
            for(int j = 0;j < m;j++)
            {
                scanf("%c",&map[i][j]);
                if(map[i][j] == ‘r‘)
                {
                    sx = i;
                    sy = j;
                }
            }
            getchar();
        }
        if(bfs())
        {
            printf("%d\n",ans);
        }
        else
        {
            printf("Poor ANGEL has to stay in the prison all his life.\n");
        }
    }

    return 0;
}
时间: 2024-08-27 07:51:31

Rescue的相关文章

[ACM] hdu 1242 Rescue (BFS+优先队列)

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:

poj1122 FDNY to the Rescue!(dij+反向建图+输出路径)

题目链接:poj1122 FDNY to the Rescue! 题意:给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路口之间没有直接路径,再给出火警位置所在的交叉路口 和 一个或多个消防站所处的交叉路口位置.输出要求按消防站到火警位置所需时间从小到大排列,输出信息包括消防站位置(初始位置),火警位置(目标位置),所需时间,最短路径上每个交叉路口. 题解:反向建图,从火警位置求一次最短路,求最短路时记录路径,按时间从小到大输出. 1 #in

HDU 4057 Rescue the Rabbit (AC自动机+DP)

http://acm.hdu.edu.cn/showproblem.php?pid=4057 Rescue the Rabbit Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1482    Accepted Submission(s): 430 Problem Description Dr. X is a biologist,

HDU1242 Rescue 【BFS】

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16314    Accepted Submission(s): 5926 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is

HDUJ 1242 Rescue 搜索

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15582    Accepted Submission(s): 5656 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is

杭电 1242 Rescue(广搜)

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): 15597    Accepted Submission(s): 5663 Problem Description Angel was caught by the MOLIGPY!

sdut 2603 Rescue The Princess(算是解析几何吧)(山东省第四届ACM省赛A题)

题目地址:sdut 2603 Rescue The Princess Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Several days ago, a beast caught a beautiful princess and the princess was put in prison. To rescue the princess, a prince who wanted to marry the princess

齐天大圣老司机亲传rescue恢复磁盘分区

老葵花哥哥课堂开课了 本文档秉承爱看不看的原则 一不要钱 二服务大众的高尚情操 咱们今天讲一讲rescue恢复磁盘分区 首先咱们搭建环境搞起来 (parted) mkpart  #创建分区 Partition name?  []? oldboy #名字 File system type?  [ext2]? ext4  #类型 (本人属于帅的类型) Start? 50  (开始) End? 70     (结束) 按这个方法 做两个 Number  Start   End     Size    

error:no such partition grub rescue

重新安装了ubuntu12.04后,Ubuntu开机就出现:error:no such partitiongrub rescue >一般情况下,出现这类错误是引导文件出错或者系统找不到引导文件,而系统并没有坏,所以不用重新安装系统.需要进行如下的设置.一 grub介绍grub是一个引导管理程序,它允许用户可以在计算机内同时拥有多个操作系统,并在计算机启动时选择希望运行的操作系统.GRUB可用于选择操作系统分区上的不同内核,也可用于向这些内核传递启动参数.[引导过程]由硬盘启动时,BIOS通常是转

POJ 1122.FDNY to the Rescue!

FDNY to the Rescue! Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2808   Accepted: 860 Description The Fire Department of New York (FDNY) has always been proud of their response time to fires in New York City, but they want to make the