HDU-1242 Rescue

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

HDU-1242 Rescue

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)

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


题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1242

分析

题意:大概题意就是天使被关起来了,然后天使的朋友要救天使,在一个M*M的矩阵中,天使被关在一个位置,天使的朋友要从自己的位置走到天使的位置,每走一步要用一个单位的时间,如果碰到守卫就要杀死他,杀死守卫也要要用一个单位的时间,最后输出所用的最少的时间,如果到达不了天使的位置,输出“Poor ANGEL has to stay in the prison all his life.”

思路:首先呢,这是一道广搜题,我用到了优先级队列,因为杀死守卫浪费1个单位时间,就代表了走到守卫的位置用了2个单位时间,所以要对每次进队的位置根据步数进行排序,然后从天使朋友的位置开始搜索周围的4个方向,先判断是否可走,如果可走,就让步数+1,碰到守卫步数+2并标记为已走,然后进队,如果不可走就直接跳过,4个方向都判断过后再从队首表示的位置开始搜索,依次搜索下去,直到走到天使的位置,如果队为空就表示走不到天使的位置。

代码
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
char dt[200][200];
int m,n,xl,xt,yl,yt;
int xy[4][2]={1,0,0,-1,0,1,-1,0},temp[200][200];
struct cmp
{
    int i,j,pre;
    friend bool operator < (cmp a,cmp b)
    {
        return a.pre>b.pre;
    }
};
priority_queue<cmp>it;
int DFS(int x,int y)
{
    memset(temp,0,sizeof(temp));
    cmp e,o;
    e.i=xl;
    e.j=yl;
    e.pre=0;
    it.push(e);
    temp[e.i][e.j]=1;
    while (!it.empty())
    {
        e=it.top();
        it.pop();
        if (dt[e.i][e.j]=='r')
        {
            return e.pre;
        }
        for (int t=0;t<4;t++)
        {
            o=e;
            o.i+=xy[t][0];
            o.j+=xy[t][1];
            if (dt[o.i][o.j]=='#'||o.i>=m||o.j>=n||o.i<0||o.j<0||temp[o.i][o.j]==1)
                continue;
            o.pre++;
            if (dt[o.i][o.j]=='x')
                o.pre++;
            temp[o.i][o.j]=1;
            it.push(o);
        }
    }
    return 0;
}
int main()
{
    while (scanf("%d %d",&m,&n)!=EOF)
    {
        temp[200][200]={0};
        for (int k=0;k<m;k++)
        {
            getchar();
            for (int l=0;l<n;l++)
            {
                scanf("%c",&dt[k][l]);
                if (dt[k][l]=='a')
                {
                    xl=k;
                    yl=l;
                }
            }
        }
        int num=DFS(xl,yl);
        if (num)
            printf("%d\n",num);
        else
            printf("Poor ANGEL has to stay in the prison all his life.\n");
    }
    return 0;
}
题解—Angel was caught by theMOLIGPY! Problem DescriptionAngel was caught by theMOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M &amp;lt;= 200) matrix. There are WALLs, ROADs, and GUARDs in theprison.&lt;... 阅读详情

相关推荐

HDU 1242 Rescue(BFS)

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 &lt;= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Ange...

AAS48的博客 277

ZOJ 1649: Rescue(BFS)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=649 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 ...

388

A - Rescue

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M &lt;= 200) matrix. There are WALLs, ROADs, and GUARDs in the prisonAngel's friends want to...

hnlg311709000526的博客 487

ZOJ - 1649 Rescue(dfs)

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 ...

起名好难呀! 299

HDU-1242-Rescue

HDU-1242-Rescue http://acm.hdu.edu.cn/showproblem.php?pid=1242 bfs即可,可能有多个’r’,而’a’只有一个,从’a’开始搜,找到的第一个’r’即为所求 需要注意的是这题宽搜时存在障碍物,遇到’x’点是,时间+2,如果用普通的队列就并不能保证每次出队的是时间最小的元素,所以要用优先队列,第一次用优先队列,还不熟练哇 优先队列(

菜鸟的程序人生 4267

HDU 1242 Rescue营救 BFS算法

HDU 1242 Rescue营救 BFS算法

一蓑烟雨 952

HDU-1242 Rescue BFS

HDU-1242   Rescue Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M &lt;= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Ange...

aero1009的博客 238

hdu 1242 Rescue

hdu  1242  Rescue             题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242 BFS 题目大意:angel被魔族人抓走了,朋友们要前去营救。给出地图,图中有五种符号,分别是'.'-Road、'#'-Wall、'r'-Friends、'x'-Guards、'a'-Angel。遇到守卫要花一单位的时间干掉他。

slicer的技术交流栈 764

HDU - 1242-Rescue(bfs)

HDU - 1242-Rescue 题目大意:给你一个n*m的矩阵,矩阵中’a’代表天使,'r’代表天使的朋友,‘x’代表守卫(虽然题目没说,可以根据样例推理出来),’.‘代表可以走的路,’#'代表不能走的路,每走一步花费1个单位时间,而遇到守卫要花费额外的1个单位时间打败它。 解题思路:很典型的bfs,和我比赛时的另一题差不多。题目要求天使朋友解救天使所花费最小时间,注意,输入样例之中已经明确表...

weixin_43871404的博客 183

hdu1242 Rescue

hdu1242 Rescue 广搜的模板 弄明白之后就觉得……太简单了 #include #include using namespace std; char mp[201][201]; int m,n,dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; struct point { int x,y,step; friend bo

Time flies 893

hdu 1242 Rescue

Rescue<br />http://acm.hdu.edu.cn/showproblem.php?pid=1242<br />Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/

jw72jw的专栏 1340

HDU T1242 Rescue

                               HDU T1242 Rescue  题解:     基础题,可是鄙人因为有处if没加花括号而找了好久的bug,粗心了......     第一次看到这题的时候,以为天使有多个朋友,就从天使开始向外遍历,其实都差不多,一股清流......     因为数据范围比较小,所以dfs和bfs都可以写。 Dfs写法 #include...

Mr__Charles的博客 357

hdu 1242 Rescue (BFS+优先队列)

hdu 1242 Rescue (BFS+优先队列) 题意 一个地图中,给定起点和终点,上下左右走,每走一步花1个单位时间,遇到怪,需要杀死怪,额外花1个单位时间,求从起点到终点最少花多少分钟。 解题思路: 主要思路是广度优先搜索,但因为其中有怪,需要额外花时间,那么对于队列里面的每个节点(保存x,y坐标和当前所用时间),要想最少花时间,那么每次在队头取出的元素应该是队列里面所用时间最少的,所以应该用优先队列。 (我最开始就就是一直没有优先队列,就一直答案不太对,看了别人的代码才用最优队列,才过) #inc

wangjunjie_123的博客 375

HDU 1242 Rescue(BFS或BFS+优先队列)

HDU 1242 Rescue(BFS或BFS+优先队列) http://acm.hdu.edu.cn/showproblem.php?pid=1242 题意:有个R*C的迷宫,里面有一个a和多个r和多个x,现在你要从这多个r点走到a点去,且如果各自是x,则要花时间消灭守卫,即多花1分钟.问你从任意r点到a的最少时间是多少? 分析:        网上题解多说用优先队列,但是对于为什么用优

code 1394

HDU 1242 Rescue

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

M阳光的专栏 625

hdu1242Rescue

 STL容器之优先队列 优先级队列,以前刷题的时候用的比较熟,现在竟然我只能记得它的关键字是priority_queue(太伤了)。在一些定义了权重的地方这个数据结构是很有用的。 先回顾队列的定义:队列(queue)维护了一组对象,进入队列的对象被放置在尾部,下一个被取出的元素则取自队列的首部。priority_queue特别之处在于,允许用户为队列中存储的元素设置优先级。这种队列不

nyist_xsk 267

杭电 HDU 1242 Rescue

http://acm.hdu.edu.cn/showproblem.php?pid=1242 问题:牢房里有墙(#),警卫(x)和道路( . ),天使被关在牢房里位置为a,你的位置在r处,杀死一个警卫要一秒钟,每走一步要一秒钟,求最短时间救出天使,不能救出则 输出:Poor ANGEL has to stay in the prison all his life.  求最短路径,果断广搜B

蚕豆儿 1491
上一篇: HDU-1241 Oil Deposits
下一篇: HDU-1312 Red and Black
tjial
博客等级 码龄9年 26粉丝 149原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值