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;
}


渣渣

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值