蓝桥杯 C/C++ 大学B组 试题E 迷宫(超详细注释版)(BFS+回溯)

蓝桥杯 C/C++ 大学B组 试题E 迷宫(超详细注释版)(BFS+回溯)

【问题描述】

下图给出了一个迷宫的平面图,其中标记为 1 的为障碍,标记为 0 的为可 以通行的地方。
010000

000100

001001

110000
迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个位置走到这 个它的上、下、左、右四个方向之一。 对于上面的迷宫,从入口开始,可以按DRRURRDDDR 的顺序通过迷宫, 一共 10 步。其中 D、U、L、R 分别表示向下、向上、向左、向右走。 对于下面这个更复杂的迷宫(30 行 50 列),请找出一种通过迷宫的方式, 其使用的步数最少,在步数最少的前提下,请找出字典序最小的一个作为答案。 请注意在字典序中D<L<R<U。
01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000

题目分析:

这题是第十届蓝桥杯省赛的E题,一个迷宫问题,要求找出字典序的最短路径,解法比较直接,但是bfs写的少的话,可能会有一点难度,主要使用的就是带路径的bfs。搜索完BFS之后,从终点往起点回溯的求解路径。
得到的答案为:
DDDDRRURRRRRRRDRRRDDDLDDRDDDDDDDDDDDDRDRDRRUUURRRRDDDDRDRRRRRURRRDRRDDDRRRRUURUUUUUUUULLLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDRDRRRRDRDRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR
共186位,即186步

超详细注释版题解:

#include<iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <queue>
#include <map>
using namespace std;
char maze[30][50]={0};//存储迷宫信息
char visit[30][50];//访问数组,同时存放到达这个位置时所走的方向
typedef struct ds{//自定义一个结构体,记录行,列,已经走到当前位置的走的方向
    int row;
    int col;
    char director;
}ds;
queue<ds> bfs_que;//bfs使用的待搜索队列
int next_dir[4][2]={//存放4个方向,用于控制下一步的走向,字典序排列
        //D,L,R,U
        {1,0},
        {0,-1},
        {0,1},
        {-1,0}
};
char dir[4] = {'D','L','R','U'};//存放字典序的四个方向
map<char,int> dir_index;
bool is_ok(int row, int col){//判断是否可以走
    return (row >= 0 && row < 30 && col >= 0 && col < 50 && (visit[row][col] == char(0) ) && (maze[row][col] == '0') ) ? true : false;
}
void bfs(){
    ds start={0,0,'S'};//(0,0)为起点,这里面的director设为‘S’,表示起点
    bfs_que.push(start);//将起点加入待搜索队列
    ds now;//当前的结点
    now = bfs_que.front();//取带搜索队列中的队头
    visit[now.row][now.col] = 'S';
    bfs_que.pop();//取队头之后将队头pop出去
    while(!(now.row == 29 && now.col == 49)){//因为题目给的是有解的,我们这里就当未到终点时,一直搜索
        for(int i = 0; i < 4; i++){//按照字典序尝试四个方向是否可以走
            if(is_ok(now.row + next_dir[i][0],now.col + next_dir[i][1])){//如果这个方向的下一步可以走,就将它的信息加入待搜索队列
                ds mid = {now.row + next_dir[i][0],now.col + next_dir[i][1],dir[i]};//存入mid中
                bfs_que.push(mid);//将mid放入待搜索队列
            }
        }
        if(!(now.row == 29 && now.col == 49)){
            now = bfs_que.front();//取待搜索队列的队头
//            cout<<now.row<<" "<<now.col<<" "<<now.director<<" "<<bfs_que.size()<<endl;
            visit[now.row][now.col] = now.director;
            bfs_que.pop();//将队头pop
        } else
            break;
    }
}
int main(){
    fstream in;//文件流,这里面直接读取迷宫的txt文件
    in.open("/Users/qiguan/maze.txt");
    //读取文件
    for(int i = 0; i < 30; i++){
        for(int j = 0 ; j < 50; j++) {
            in >> maze[i][j];
        }
    }
    bfs();
    //D L R U的字典序对应的数组下标
    dir_index['D'] = 0;
    dir_index['L'] = 1;
    dir_index['R'] = 2;
    dir_index['U'] = 3;
    string ans = "";//用于存放路径
    int row = 29, col = 49;//从终点往起点回溯
    while(!(row == 0 && col == 0)){//回溯求解路径
        ans = ans + visit[row][col];
        char dir = visit[row][col];
        row = row - next_dir[dir_index[dir]][0];//减去来时候的路径
        col = col - next_dir[dir_index[dir]][1];
    }
    reverse(ans.begin(),ans.end());//得到的字符串进行逆序
    cout<<ans;//这就是需要的路径信息
    return 0;
}
发布了97 篇原创文章 · 获赞 50 · 访问量 8167

猜你喜欢

转载自blog.csdn.net/qq_43422111/article/details/103240259