zoukankan      html  css  js  c++  java
  • 蓝桥杯 算法提高 学霸的迷宫 经典BFS问题

      算法提高 学霸的迷宫  
    时间限制:1.0s   内存限制:256.0MB
        
    问题描述
      学霸抢走了大家的作业,班长为了帮同学们找回作业,决定去找学霸决斗。但学霸为了不要别人打扰,住在一个城堡里,城堡外面是一个二维的格子迷宫,要进城堡必须得先通过迷宫。因为班长还有妹子要陪,磨刀不误砍柴功,他为了节约时间,从线人那里搞到了迷宫的地图,准备提前计算最短的路线。可是他现在正向妹子解释这件事情,于是就委托你帮他找一条最短的路线。
    输入格式
      第一行两个整数n, m,为迷宫的长宽。
      接下来n行,每行m个数,数之间没有间隔,为0或1中的一个。0表示这个格子可以通过,1表示不可以。假设你现在已经在迷宫坐标(1,1)的地方,即左上角,迷宫的出口在(n,m)。每次移动时只能向上下左右4个方向移动到另外一个可以通过的格子里,每次移动算一步。数据保证(1,1),(n,m)可以通过。
    输出格式
      第一行一个数为需要的最少步数K。
      第二行K个字符,每个字符∈{U,D,L,R},分别表示上下左右。如果有多条长度相同的最短路径,选择在此表示方法下字典序最小的一个。
    样例输入
    Input Sample 1:
    3 3
    001
    100
    110

    Input Sample 2:
    3 3
    000
    000
    000
    样例输出
    Output Sample 1:
    4
    RDRD

    Output Sample 2:
    4
    DDRR
    数据规模和约定
      有20%的数据满足:1<=n,m<=10
      有50%的数据满足:1<=n,m<=50
      有100%的数据满足:1<=n,m<=500。
     
    #include <iostream>
    #include <algorithm>
    #include <cstdlib>
    #include <utility>
    #include <map>
    #include <cstdio>
    #include <queue>
    using namespace std;
    
    const int maxn = 600 + 10;
    const int INF = 100000000;
    typedef pair<int,int> P;
    // D(下), L(左), R(右), U(上) 
    int dir[4][2] = { {1, 0}, {0, -1}, {0, 1}, {-1, 0}};
    char dir_c[4] = {'D', 'L', 'R', 'U'};
    int row, col;               //行列 
    char maze[maxn][maxn];      //表示迷宫的字符串的数组 
    int d[maxn][maxn];          //到各个位置的最短距离的数组 
    string Min;                 //U,D,L,R
    queue<P> que;             
    void input();        
    bool judge(int r, int c);
    int BFS();
    
    void input()
    {
        scanf("%d%d", &row, &col);
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                cin >> maze[i][j];
            }
        }
        //所有位置初始化
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                d[i][j] = INF;
            }
        } 
    }
    
    bool judge(int r, int c)
    {
        return (r >= 0 && r < row) && (c >= 0 && c < col)
                && (maze[r][c] != '1');  //可走 
    }
    
    int BFS()
    {
        //将起点假如队列, 并把这一地点的距离设置为 0
        que.push(P(0, 0));
        queue<string> path;
        path.push("");
        
        d[0][0] = 0;
        Min = "";
        
        while (!que.empty())
        {
            P p = que.front(); que.pop();
            string t = path.front(); path.pop();
            
            if (p.first == row - 1 && p.second == col - 1) {
                Min = t;             //因为我的方向就是按照字典序 DLRU,所以这时候形成最短路线的路径就是按照字典序最小的路线! 
                break;
            }
            
            for (int i = 0; i < 4; i++) {
                //移动之后的位置为(nx,ny)
                int nx = p.first + dir[i][0], ny = p.second + dir[i][1];
                
                //可以走,且尚未访问(d[nv][ny]==INF
                if (judge(nx, ny) && d[nx][ny] == INF) {
                    //加入到队列,并且到该位置的距离确定为到p的距离+1
                    que.push(P(nx, ny));
                    path.push(t + dir_c[i]);                 //这里数据结构组织的并不好,我应该一开始就把路径和位置组合成结构体,会更方便 
                    d[nx][ny] = d[p.first][p.second] + 1;    //因为是的方向就是按照 DLRU字典序遍历,所以不需要有什么额外的判断,只需要和行走路线 
                    maze[nx][ny] = '1';                      //一起出队,入队就可以了! 
                } 
            }
        }
        return d[row - 1][col - 1];
    }
    
    void solve()
    {
        input();
        int res = BFS();
        printf("%d
    %s
    ", res, Min.c_str());
    }
    
    int main()
    {
        solve();
        return 0;
    }
  • 相关阅读:
    简单拓扑排序
    Havel-Hakimi定理
    阿里云宁磊:能力中心开启,携手伙伴共享共赢
    阿里云高磊:API网关加速能力聚合与生态集成
    阿里云智能推荐AIRec产品介绍
    OpenSearch最新功能介绍
    30分钟全方位了解阿里云Elasticsearch
    研发效能提升 36 计第三课:束水攻沙,持续加快产品交付速度
    SaaS上云工具包为企业应用构筑上云之梯
    阿里云资深技术专家黄省江:让天下没有难做的SaaS
  • 原文地址:https://www.cnblogs.com/douzujun/p/6661496.html
Copyright © 2011-2022 走看看