ZOJ 1649:Rescue(BFS)

Rescue


Time Limit: 2 Seconds      Memory Limit: 65536 KB



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

这题是说Angle被人抓住了,她的朋友去救她。。而x是阻碍他的敌人,但是Angle的朋友足够强,能消灭所有敌人,但是在消灭敌人的同时,

他要多花费1秒的时间,题目要你求出他拯救Angle所需的最短时间。。

这道题用BFS,但要注意的是最短路径也可能并不是最优解。。

所以我们用定义一个二维数组来记录每个位置时的最短时间,在BFS搜索的过程中,到某个位置时,搜索的结果大于最短时间时,不入队。

这样就解决了本题的问题了。

此外,不要一判断到达目的位置时就推出BFS返回结果。。我就是因为在这里WA了一发。。因为这可能不是最优解。。只有等到队列为空或

BFS搜索结束时,才能得出最优解或者是“无法到达目的位置”。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath>

using namespace std;

const int maxn = 205;
const int INF  = 100000;//用于初始每个位置的时间,尽可能开大点
char map[maxn][maxn];//地图
int  mintime[maxn][maxn];//记录每个位置的最小时间
int n, m;//地图的大小
int ax, ay;//Angel的位置
int fx, fy;//Angel的friend的位置
int ans;//结果
//地图的走法
int dx[] = { -1, 0, 0, 1 };
int dy[] = { 0, -1, 1, 0 };

struct point
{
    int x;
    int y;//方格的位置
    int time;//记录当前走到这所花费的时间
};
queue<point>Q;//队列

int BFS()//BFS函数
{
    while( !Q.empty() )
    {
        point temp = Q.front();//记录队首元素
        Q.pop();
        // if( temp.x==ax && temp.y==ay )
        //return mintime[ax][ay]; 开始我在这输出,使得WA。。因为这可能不是最优解
        for(int i=0; i<4; i++)
        {
            int xx = temp.x + dx[i];
            int yy = temp.y + dy[i];
            if( xx>=0 && xx<n && yy>=0 && yy<m && map[xx][yy]!='#' )//排除边界以及墙壁
            {
                point next;//下一个位置
                next.x = xx;
                next.y = yy;
                next.time = temp.time + 1;
                if( map[xx][yy] == 'x' ) next.time += 1;//若遇到敌人,消灭其需加1
                if( next.time < mintime[xx][yy] )//若这种走法所用的时间比原来mintime小则入队
                {
                    mintime[xx][yy] = next.time;
                    Q.push(next);
                }
            }
        }
    }
    return mintime[ax][ay];//返回结果
}

void output()//输出
{
    if( ans < INF )
        printf("%d\n", ans);
    else
        printf("Poor ANGEL has to stay in the prison all his life.\n");
}

void init()//输入以及初始化
{
    while(scanf("%d%d", &n, &m)==2)
    {
        memset( map, 0, sizeof(map) );
        for(int i=0; i<n; i++)
            scanf("%s", map[i]);//读入地图
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
            {
                mintime[i][j] = INF;//初始每个位置的时间
                if( map[i][j] == 'r' ) //记录friend的位置
                {
                    fx = i;
                    fy=j;
                }
                if( map[i][j] == 'a' )//记录Angel的位置
                {
                    ax = i;
                    ay = j;
                }
            }
        point stay;
        stay.x = fx;
        stay.y = fy;
        stay.time = 0;
        mintime[fx][fy] = 0;
        Q.push( stay );//BFS的开始
        ans = BFS();
        output();
    }
}

int main()
{
    init();

    return 0;
}
时间: 2024-10-23 09:07:45

ZOJ 1649:Rescue(BFS)的相关文章

zoj 2913 Bus Pass (BFS)

Bus Pass Time Limit: 5 Seconds      Memory Limit: 32768 KB You travel a lot by bus and the costs of all the seperate tickets are starting to add up. Therefore you want to see if it might be advantageous for you to buy a bus pass. The way the bus syst

Rescue(bfs)

Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 24   Accepted Submission(s) : 11 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is describ

ZOJ 3420 Double Maze (BFS)

链接 :  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3420 普通的BFS 两个图的状态表示成一个状态.记录答案直接用string保存操作. #include <iostream> #include <sstream> #include <cstring> #include <cstdio> #include <vector> #include <stac

Sicily 1709:PropBot(BFS)

#include<bits/stdc++.h> using namespace std; int TIME; double X, Y; double MIN; double MIN_X, MIN_Y; double SQRT = sqrt(0.5); double DIR[8][2] = {{1,0}, {SQRT, -1*SQRT}, {0, -1}, {-1*SQRT, -1*SQRT}, {-1, 0}, {-1*SQRT, SQRT}, {0, 1}, {SQRT, SQRT}}; m

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+

zoj 3811 Untrusted Patrol(bfs或dfs)

Untrusted Patrol Time Limit: 3 Seconds      Memory Limit: 65536 KB Edward is a rich man. He owns a large factory for health drink production. As a matter of course, there is a large warehouse in the factory. To ensure the safety of drinks, Edward hir

HDU 1242——Rescue(优先队列)

题意: 一个天使a被关在迷宫里,她的许多小伙伴r打算去救她,求小伙伴就到她需要的最小时间.在迷宫里有守卫,打败守卫需要一个单位时间,如果碰到守卫必须要杀死他 思路: 天使只有一个,她的小伙伴有很多,所以可以让天使找她的小伙伴,一旦找到小伙伴就renturn.时间小的优先级高.优先队列搞定 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<

hdu 1026 Ignatius and the Princess I(bfs)

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16269    Accepted Submission(s): 5164 Special Judge Problem Description The Princess has been abducted by the BEelzeb