zoukankan      html  css  js  c++  java
  • hdu 1242 c++ 广搜

      这应该算是一道很不错的搜索题了,考察很全面,细致,新手应该努力去尝试做做;

    Angel被传说中神秘的邪恶的Moligpy人抓住了!他被关在一个迷宫中。迷宫的长、宽不超过200。 迷宫中有不可以越过的墙以及监狱的看守。

    Angel的朋友带了一些救援队来到了迷宫中。他们的任务是:接近Angel。我们假设接近Angel就是到达Angel所在的位置。

    假设移动需要1单位时间,杀死一个看守也需要1单位时间。到达一个格子以后,如果该格子有看守,则一定要杀死。交给你的任务是,最少要多少单位时间,才能到达Angel所在的地方?(只能向上、下、左、右4个方向移动)

    该题含有多组测试数据。每组测试数据第一行二个整数n,m。表示迷宫的大小为n*m。 以后n行,每行m个时字符。其中“#”代表墙,“.”表示可以移动,“x”表示看守,“a”表示Angel,“r”表示救援队伍。

    Output

    一行,代表救出Angel的最短时间。如果救援小组永远不能达到Angel处,则输出“Poor ANGEL has to stay in the prison all his life.”

    首先搜索时不知道有多少朋友,在什么地方,所以应该从angel 开始倒着搜,题目中有看守(障碍物),步骤应该额外加一,所以普通的广搜队列已经不能解决这个问题了,应该用优先队列(不懂得看看优先队列用法及思想)

    #include<iostream>
    #include<cstdio>
    #include<queue>
    using namespace std;
    char s[202][202];
    int n,m,bx,by;
    bool remark;
    int bam[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
    struct node
    {
    int x,y,step;
    friend bool operator < (node t1,node t2)
    {
    return t1.step>t2.step;
    }
    };
    void bfs(int bx,int by)//广搜
    {
    remark = 0;
    priority_queue<node>qu;//优先队列
    node t;
    t.x=bx,t.y=by,t.step=0;
    qu.push(t);
    while(!qu.empty()) // 判断队列是否为空
    {
    t=qu.front();
    qu.pop();
    for(int i = 0;i < 4;i ++)// 四个方向逐一搜索
    {
    node point =t;

    point.step++;
    point.x+=bam[i][0];
    point.y+=bam[i][1];
    if(point.x<=n && point.x>0 && point.y <= m && point.y > 0 && s[point.x][point.y]!='#')
    {
    if(s[point.x][point.y]=='r')
    {
    cout<<point.step<<endl;
    remark = 1;//标记
    return;
    }
    if(s[point.x][point.y]=='x')
    point.step++;

    s[point.x][point.y]='#';//将搜索过的置为墙,很是巧妙
    qu.push(point);
    }
    }
    }
    }
    int main()
    {
    int i,j;
    while(cin>>n>>m)
    {
    for(i=1;i<=n;i++)
    for(j=1;j<=m;j++)
    {
    cin>>s[i][j];
    if(s[i][j]=='a')
    bx=i,by=j;
    }

    bfs(bx,by);
    if(!remark) //
    cout<<"Poor ANGEL has to stay in the prison all his life.\n";
    }
    }


    }
    };

  • 相关阅读:
    apache http server 和tomcat的区别 以及nginx
    2020-2-12 这样提升自己的口才
    两种常用的队列
    栈的实现与应用
    线性表
    Nginx实现虚拟主机
    将apache添加到服务
    apache安装
    最小生成树
    图的深度优先搜索
  • 原文地址:https://www.cnblogs.com/lfyy/p/2696653.html
Copyright © 2011-2022 走看看