HDU 搜索练习 Rescue

Rescue

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 44 Accepted Submission(s) : 13

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.<br><br>Angel‘s friends want to save Angel. Their task is: approach Angel. We assume that &quot;approach Angel&quot; 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.<br><br>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.)<br>

Input

First line contains two integers stand for N and
M.<br><br>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. <br><br>Process to the end of the file.<br>

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

Sample Input

7 8

#.#####.

#.a#..r.

#..#x...

..#..#.#

#...##..

.#......

........

Sample Output

13

简单题意:

  求解R到达是否能到达A处,能的话,最少需要几步

思路分析:

  同其他题目一样,有目标bfs了

# include <iostream>
# include <cstring>
# include <queue>
# include <fstream>
using namespace std;
struct Info
{
    int x, y;
}a, r;
int n, m;
char map[500][500];

int dir[4][2] = {{-1, 0},{1, 0},{0, -1},{0, 1},};
int jishu[500][500];

bool border(int x, int y)
{
    if(x >= 1 && x <= n && y >= 1 && y <= m)
    return true;
    return false;
}
int bfs();
int main()
{
    //fstream cin("aaa.txt");
    while(cin >> n >> m)
    {

        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                cin >> map[i][j];
                if(map[i][j] == ‘a‘)
                {
                    a.x = i;
                    a.y = j;
                }
                if(map[i][j] == ‘r‘)
                {
                    r.x = i;
                    r.y = j;
                }
            }
        }
        int haha = bfs();
        if(haha == 88888888)
        cout << "Poor ANGEL has to stay in the prison all his life." << endl;
        else
        cout << haha << endl;
    }
    return 0;
}
int bfs()
{
    for(int i = 0; i < 500; i++)
    for(int j = 0; j < 500; j++)
    jishu[i][j] = 88888888;

    jishu[r.x][r.y] = 0;

    queue <Info> Q;
    Q.push(r);
    Info t, tep;
    while(!Q.empty())
    {
        tep = Q.front();
        Q.pop();
        if(tep.x == a.x && tep.y == a.y)
        {
            break;
        }

        for(int i = 0; i < 4; i++)
        {
            t.x = tep.x + dir[i][0];
            t.y = tep.y + dir[i][1];
            if(!border(t.x, t.y))// 出界, 走过, 石墙
            continue;

            if(map[t.x][t.y] == ‘#‘)
            continue;

            int min;
            if(map[t.x][t.y] == ‘x‘)
                min = jishu[tep.x][tep.y] + 2;
            else
                min = jishu[tep.x][tep.y] + 1;

            if(min >= jishu[t.x][t.y])
                continue;
            jishu[t.x][t.y] = min;

            //cout << jishu[t.x][t.y] << endl;

            Q.push(t);
        }
    }
    return jishu[a.x][a.y];
}
时间: 2024-09-29 15:51:46

HDU 搜索练习 Rescue的相关文章

HDU 搜索练习 Oil Deposits

Oil Deposits Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 61 Accepted Submission(s) : 25 Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground oil

HDU 搜索练习 连连看

连连看 Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 27989 Accepted Submission(s): 6952 Problem Description “连连看”相信很多人都玩过.没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子.如果某两个相同的棋子,可以通过一条线连起来(这条线不能经过其它棋子),而

HDU 搜索练习 The magic apple tree

The magic apple tree Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Submission(s) : 26 Accepted Submission(s) : 2 Problem Description Sailormoon girls all like eating many kinds of fruit, such as banana, grape, a

HDU 搜索练习 Red and Black

Red and Black Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 23 Accepted Submission(s) : 13 Problem Description There is a rectangular room, covered with square tiles. Each tile is colored either

HDU 搜索练习 非常可乐

非常可乐 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 51 Accepted Submission(s) : 21 Problem Description 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一

HDU 搜索练习 Catch him

Catch him Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 797 Accepted Submission(s): 361 Problem Description 在美式足球中,四分卫负责指挥整只球队的进攻战术和跑位,以及给接球员传球的任务.四分卫是一只球队进攻组最重要的球员,而且一般身体都相对比较弱小,所以通常球队会安排5-7名大汉来

HDU 搜索练习 Prime Ring Problem

Prime Ring Problem Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 17 Accepted Submission(s) : 10 Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..

HDU 搜索练习 N皇后问题

N皇后问题 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 39 Accepted Submission(s) : 18 Problem Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上.<br>你的任务是,对于给定的N,求出有多少种合

HDU 搜索练习 A strange lift

A strange lift Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 122 Accepted Submission(s) : 22 Problem Description There is a strange lift.The lift can stop can at every floor as you want, and ther