BFS HDOJ 1242 Rescue

题目传送门

题意:从r走到a,遇到x多走一步,问最小走到a的步数

分析:因为r有多个,反过来想从a走到某个r的最小步数,简单的BFS。我对这题有特殊的感情,去年刚来集训队时肉鸽推荐了这题,当时什么都不会,看个数组模拟队列的BFS看的头晕,现在看起来也不过如此,额,当年开始是从r走到a的,因为数据巨弱才过的,应该要用到优先队列。

/************************************************
* Author        :Running_Time
* Created Time  :2015/9/25 星期五 09:13:51
* File Name     :B_BFS.cpp
 ************************************************/

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 2e2 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-8;
struct Angle    {
    int x, y, step;
    Angle () {}
    Angle (int x, int y, int step) : x (x), y (y), step (step) {}
};
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
bool vis[N][N];
char maze[N][N];
int n, m;

bool judge(int x, int y)    {
    if (x < 1 || x > n || y < 1 || y > m || vis[x][y] || maze[x][y] == ‘#‘) return false;
    else    return true;
}

void BFS(Angle s)    {
    int ret = INF;
    memset (vis, false, sizeof (vis));
    queue<Angle> Q; Q.push (s);
    vis[s.x][s.y] = true;
    while (!Q.empty ()) {
        Angle r = Q.front ();   Q.pop ();
        for (int i=0; i<4; ++i) {
            int tx = r.x + dx[i];
            int ty = r.y + dy[i];
            if (!judge (tx, ty))  continue;
            vis[tx][ty] = true;
            if (maze[tx][ty] == ‘r‘)    {
                ret = min (ret, r.step + 1);    continue;
            }
            else if (maze[tx][ty] == ‘x‘)   {
                Q.push (Angle (tx, ty, r.step + 2));    continue;
            }
            else    Q.push (Angle (tx, ty, r.step + 1));
        }
    }
    if (ret == INF) puts ("Poor ANGEL has to stay in the prison all his life.");
    else    printf ("%d\n", ret);
}

int main(void)   {
    while (scanf ("%d%d", &n, &m) == 2) {
        for (int i=1; i<=n; ++i)    {
            scanf ("%s", maze[i] + 1);
        }
        bool find = false;  Angle start;
        for (int i=1; i<=n && !find; ++i)    {
            for (int j=1; j<=m; ++j)    {
                if (maze[i][j] == ‘a‘)  {
                    start = Angle (i, j, 0);
                    find = true;    break;
                }
            }
        }
        BFS (start);
    }

    return 0;
}

当年的代码不忍直视。。。

#include<stdio.h>
typedef struct point
{
    int x,y,step;
}target;
int N,M,dir[4][2]={0,1,0,-1,1,0,-1,0},ax,ay;
int flag[202][202];
char map[302][302];
target que[40005];
int BFS(target start)
{
    int end,top,i;
    int min=1000000;
    target in,next;
    end=top=0;
    que[top]=start;
    while (top>=end)
    {
        in=que[end];
        end=(end+1);
        for (i=0;i<4;i++)
        {
            next.x=in.x+dir[i][0];
            next.y=in.y+dir[i][1];
            if (map[next.x][next.y]==‘r‘)
            {
                if (min>in.step+1)
                    min=in.step+1;
            }
            if (next.x>=0&&next.x<N&&next.y>=0&&next.y<M&&map[next.x][next.y]!=‘#‘)
            {
                if (flag[next.x][next.y]>in.step+1)
                {
                    next.step=in.step+1;
                    if (map[next.x][next.y]==‘x‘)
                        next.step++;
                    flag[next.x][next.y]=next.step;
                    top=(top+1);
                    que[top]=next;
                }
            }
        }
    }
    if (min!=1000000)return min;
    else
        return -1;
}
int main()
{
    int i,j,num;
    target start;
    while (scanf("%d%d",&N,&M)!=EOF)
    {
        for (i=0;i<N;i++)
        {
            scanf("%s",map[i]);
            for (j=0;j<M;j++)
            {
                flag[i][j]=1000000;
                if (map[i][j]==‘a‘)
                {
                    //map[i][j]=‘.‘;
                    ax=i;
                    ay=j;
                }
            }
        }
        start.x=ax;
        start.y=ay;
        start.step=0;
        //map[ax][ay]=‘.‘;
        num=BFS(start);
        if (num==-1)
            printf("Poor ANGEL has to stay in the prison all his life.\n");
        else
            printf("%d\n",num);
    }
    return 0;
}

  

时间: 2025-01-02 17:10:33

BFS HDOJ 1242 Rescue的相关文章

hdoj 1242 Rescue (BFS)

Rescue http://acm.hdu.edu.cn/showproblem.php?pid=1242 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18962    Accepted Submission(s): 6771 Problem Description Angel was caught by the MOLIGPY!

[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:

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算法

题目链接: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

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

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

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!

HDU 1242——Rescue(优先队列)

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