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

题目链接:Rescue

进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢。。。为什么不试着改变一下捏。。。。

开始以为是水题,想敲一下练手的,后来发现并不是一个简单的搜索题,BFS做肯定出事。。。后来发现题目里面也有坑

题意是从r到a的最短距离,“.”相当时间单位1,“x”相当时间单位2,求最短时间

HDU 搜索课件上说,这题和HDU1010相似,刚开始并没有觉得像剪枝,就改用  双向BFS   0ms  一Y,爽!

网上查了一下,神牛们竟然用BFS+优先队列。。。顿悟

那么本题的搜索树可以理解为root 为a,连接若干枝条,枝条的叶子就是r,那么深度大的枝条剪枝,深度最小的自然就是答案;用优先队列来控制过滤深度较大枝条,进行剪枝。

敲了一下,BFS + 优先队列   15ms  一Y,相信如果在ans的存储上优化一下,把较小的ans优先储存,0ms很轻松

送一特殊数据:

3 3

.a.

x#.

.r.

打印 4

BFS + 优先队列  代码如下

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
const int N = 210;
using namespace std;

struct node
{
    int x, y,ans;
   friend bool operator <(const node &a,const node &b)
    {
        return a.ans>b.ans;
    }
};
int n, m;
char ma[N][N];
bool vis[N][N];
int sx, sy;
int mv[4][2] = {{-1,0},{0,1},{0,-1},{1,0}};

void BFS(int sx,int sy)
{
    priority_queue<node> q;
    node f, t;
    f.x = sx, f.y = sy, f.ans = 0;
    vis[sx][sy] = true;
    q.push(f);
    while(!q.empty())
    {
        t = q.top();
         if(ma[t.x][t.y]=='r')
            {
                cout<<t.ans<<endl;
                return ;
            }
        q.pop();
        for(int i=0; i<4; i++)
        {
            f.x = t.x +mv[i][0];
            f.y = t.y +mv[i][1];
            if(!vis[f.x][f.y]&&0<=f.x&&f.x<n&&0<=f.y&&f.y<m&&ma[f.x][f.y]!='#')
            {
                if(ma[f.x][f.y]=='x')
               {
                 f.ans = t.ans+2;
                 q.push(f);
               }
               else
              {
                f.ans = t.ans+1;
                q.push(f);
              }
              vis[f.x][f.y] = true;
            }
        }
    }
     cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        bool flag = 0;
        for(int i=0; i<n; i++)
        {
           scanf("%s",ma[i]);
           if(flag)
            continue;
            for(int j=0; j<m; j++)
            {
                if(ma[i][j]=='a')
                {
                     sx=i;sy=j;flag = true;
                     break;
                }

            }
        }
        memset(vis,0,sizeof(vis));
        BFS(sx,sy);
    }
    return 0;
}

双向BFS

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
const int N = 210;
using namespace std;
int mapp[N][N];
int vis[N][N];
struct node{
    int x,y;
};
int n,m;
char ma[210][210];
int mv[4][2] = {{1,0},{0,1},{0,-1},{-1,0}};
int dis[N][N];
void BFS(int sx,int sy,int ex,int ey)
{

    queue<node>q;
    node t,f;
    memset(vis,0,sizeof(vis));
    memset(dis,0,sizeof(dis));
    f.x = sx; f.y = sy;
    t.x = ex; t.y = ey;
    vis[sx][sy] = 1;
    vis[ex][ey] = 2;
    q.push(f);
    q.push(t);
    while(!q.empty())
    {
        t = q.front();
        q.pop();
        for(int i = 0;i<4;i++)
        {
            f.x = t.x + mv[i][0];
            f.y = t.y + mv[i][1];
            if(0<=f.x && f.x <n && 0<=f.y && f.y<m)
            {
                if(ma[f.x][f.y]=='#') continue ;
                if(!vis[f.x][f.y]&& ma[f.x][f.y]=='x')
                {
                dis[f.x][f.y] = dis[t.x][t.y] + 2;
                q.push(f);
                vis[f.x][f.y] = vis[t.x][t.y];
                }
                else if(!vis[f.x][f.y]&& ma[f.x][f.y]=='.')
                {
                dis[f.x][f.y] = dis[t.x][t.y] + 1;
                q.push(f);
                vis[f.x][f.y] = vis[t.x][t.y];
                }
                else if(vis[f.x][f.y]!=vis[t.x][t.y])
                {
                    printf("%d\n",dis[f.x][f.y]+dis[t.x][t.y] + 1);
                    return ;
                }
            }
        }
    }
    cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
}
int main()
{
    int t,sx,sy,ex,ey;
    while(~scanf("%d%d",&n,&m))
    {
       for(int i = 0;i<n;i++)
       {
           scanf("%s",ma[i]);
           for(int j = 0;j<m;j++)
           {
               if(ma[i][j] == 'a')
               {
                   sx = i; sy = j;
               }
               else if(ma[i][j]=='r')
               {
                   ex = i; ey = j;
               }
           }
       }
        BFS(sx,sy,ex,ey);
    }
    return 0;
}

渣渣

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

时间: 2024-12-28 15:54:25

HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)的相关文章

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

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

[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(优先队列)

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

ZOJ 1649 &amp;&amp; HDU 1242 Rescue

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

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

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 &