HDU 1242 Rescue营救 BFS算法

题目链接:HDU 1242 Rescue营救

Rescue

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

Total Submission(s): 16524    Accepted Submission(s): 5997

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

Author

CHEN, Xue

Source

ZOJ Monthly, October 2003

题意:

Angel被关在一个监狱中,监狱可有N*M的矩阵表示,每个方格中可能有墙壁(#)、道路(.)、警卫(x)、Angel(a)和Angel的朋友(r)。Angel的朋友想到达Angel处,要么走道路,需要1的时间,要么走到警卫的格子,还需要多1的时间杀死警卫才能通行。现在需要求到达Angel处的最短时间。

分析:

求最短时间一般都用BFS搜索,但是这道题条件较多,需要酌情考虑。并不是步数最少就最好,因为杀死警卫还需要额外的时间。可以新建一个结构体point,表示当前位置和已经使用的时间。再申请一个数组mintime[][],表示走到某一格的最短时间。将可行的当前位置压入队列并持续更新mintime数组。找到Angel所在位置的mintime。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

#define INF 0xffffff
char mp[202][202];
int n, m, mintime[202][202];
int dir[4][2] = {{-1, 0},{0, 1},{1, 0},{0, -1}};

struct point
{
    int x, y, time;
};
void bfs(point s)
{
    queue<point> q;
    q.push(s);
    while(!q.empty())
    {
        point cp = q.front();
        q.pop();
        for(int i = 0; i < 4; i++)
        {
            int tx = cp.x + dir[i][0];
            int ty = cp.y + dir[i][1];
            if(tx >= 1 && tx <= n && ty >= 1 && ty <= m && mp[tx][ty] != '#')
            {
                point t;
                t.x = tx;
                t.y = ty;
                t.time = cp.time+1;
                if(mp[tx][ty] == 'x')
                    t.time++;
                if(t.time < mintime[tx][ty])
                {
                    mintime[tx][ty] = t.time;
                    q.push(t);
                }
            }
        }
    }
}
int main()
{
    char c;
    int ax, ay;
    point st;
    while(~scanf("%d%d", &n, &m))
    {
        scanf("%c", &c);
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                scanf("%c", &mp[i][j]);
                mintime[i][j] = INF;
                if(mp[i][j] == 'a')
                {
                    ax = i;
                    ay = j;
                }
                else if(mp[i][j] == 'r')
                {
                    st.x = i;
                    st.y = j;
                    st.time = 0;
                }
            }
            scanf("%c", &c);
        }
        mintime[st.x][st.y] = 0;
        bfs(st);
        if(mintime[ax][ay] < INF)
            printf("%d\n", mintime[ax][ay]);
        else
            printf("Poor ANGEL has to stay in the prison all his life.\n");
    }
    return 0;
}
时间: 2024-12-09 06:18:09

HDU 1242 Rescue营救 BFS算法的相关文章

HDU 1242 Rescue(优先队列+bfs)

题目地址:HDU 1242 这个题相比于普通的bfs有个特殊的地方,经过士兵时会额外消耗时间,也就是说此时最先搜到的时候不一定是用时最短的了.需要全部搜一遍才可以.这时候优先队列的好处就显现出来了.利用优先队列,可以让队列中的元素按时间排序,让先出来的总是时间短的,这样的话,最先搜到的一定是时间短的,就不用全部搜一遍了.PS:我是为了学优先队列做的这题..不是为了这题而现学的优先队列.. 代码如下: #include <iostream> #include <stdio.h> #i

hdu 1242:Rescue(BFS广搜 + 优先队列)

Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 14   Accepted Submission(s) : 7 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Angel was caught by the MOLIGPY

HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 开始以为是水题,想敲一下练手的,后来发现并不是一个简单的搜索题,BFS做肯定出事...后来发现题目里面也有坑 题意是从r到a的最短距离,"."相当时间单位1,"x"相当时间单位2,求最短时间 HDU 搜索课件上说,这题和HDU1010相似,刚开始并没有觉得像剪枝,就改用  双向BFS   0ms  一Y,爽! 网上查了一下,神牛们竟然用BFS+

hdu 1242 Rescue (BFS+优先队列)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1242 这道题目我是用BFS+优先队列做的.听说只用bfs会超时. 因为这道题有多个营救者,所以我们从被营救者开始bfs,找到最近的营救者就是最短时间. 先定义一个结构体,存放坐标x和y,还有到达当前点(x,y)消耗的时间. struct node { int x,y; int time; friend bool operator < (const node &a,const node &

hdu - 1242 Rescue (优先队列+bfs)

http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了. 但是别人说要用优先队列来保证时间最优,我倒是没明白,步数最优跟时间最优不是等价的吗?就算士兵要花费额外时间,可是既然先到了目标点那时间不也一定是最小的? 当然用优先队列+ a去搜索r是最稳妥的. 1 #include <cstdio> 2 #include <cstring> 3

HDU 1242 -Rescue (双向BFS)&amp;amp;&amp;amp;( BFS+优先队列)

题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出事...后来发现题目里面也有坑 题意是从r到a的最短距离,"."相当时间单位1,"x"相当时间单位2,求最短时间 HDU 搜索课件上说,这题和HDU1010相似,刚開始并没有认为像剪枝,就改用  双向BFS   0ms  一Y,爽! 网上查了一下,神牛们居然用BFS+优

HDU 1242 Rescue 营救天使

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

ZOJ 1649 &amp;&amp; HDU 1242 Rescue (BFS + 优先队列)

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

HDOJ/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 <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is: approa