JakeLin- [Blue Bridge Cup] [Algorithm Improves VIP] The Labyrinth of Xueba-Problems

Title description

Xueba snatched everyone's homework. In order to help the students get their homework, the class leader decided to go to Xueba Duel. But in order not to disturb others, Xueba lives in a castle. Outside the castle is a two-dimensional lattice maze. To enter the castle, you must first pass the maze. Because the squad leader and his sister had to accompany him, he didn't accidentally cut firewood by sharpening his knife. In order to save time, he got a map of the maze from the informant and prepared to calculate the shortest route in advance. But he is explaining this matter to his sister now, so he entrusts you to help him find the shortest route.

Input

The two integers n, m in the first line are the length and width of the maze.
In the next n lines, there are m numbers in each line, and there is no interval between the numbers, which is one of 0 or 1. 0 means this grid can pass, 1 means not. Suppose you are now at the coordinates of the maze (1,1), which is the upper left corner, and the exit of the maze is at (n, m). Each time you move, you can only move up, down, left, and right in another direction to pass through, and each move is counted as one step. The data guarantee (1,1), (n, m) can pass.

Output

The number in the first line is the minimum number of steps K required.
K characters in the second line, each character ∈ {U, D, L, R}, respectively representing up, down, left and right. If there are multiple shortest paths with the same length, select the one with the smallest lexicographic order under this representation method.

Sample input

3 3
001
100
110

Sample output

4
RDRD

Original title link: [Blue Bridge Cup] [Algorithm Improves VIP] The Labyrinth of Xueba

 

This question is a maze problem, which is solved using BFS (breadth first traversal)

  • Need to understand BFS first:
    The BFS (breadth first traversal) of the graph can be understood as the hierarchical traversal in the tree traversal, which is traversed from the starting point to the next layer, as shown below:
  • The traversal method has templates to follow:
1.建立一个队列
2.访问起始节点,将起始节点入队
3.while(队中还有元素){
访问队首的领接节点,并将它们入队(需是没有访问过的);
队首出队;
}
//直到队列为空循环结束
  • Explain the structure node
struct node{
    int x,y,h;
    string s;
    node(int a,int b,int c){
        x=a;
        y=b;
        h=c;
        s="";
    }
};
among them: 
(X, y) Indicates the horizontal and vertical coordinates of the current node
h It is the number of steps (the initial value is 0), which can be understood as the number of layers. The value is the number of layers minus 1. The number of layers of the current node is the number of layers of the previous node (the node that dequeued) +1
s A string representing the path from the starting point to the current point. The path of the current node is the path of the node at the previous layer (the node that dequeues) plus a moving letter between them
node(a,b,c) Constructor with parameters
  • Multiple paths meet the conditions

Title requirements: There are multiple paths that meet the conditions, select the smallest lexicographic order Topic requirements: There are multiple paths that meet the conditions, select the smallest lexicographic order

This requires us to traverse the neighboring nodes of the dequeuing node in the order: "D", "L", "R", "U" In
this question, I use two arrays to traverse the four directions

int dr [] = {1,0,0, -1}; // x axis movement
int dc [] = {0, -1,1,0}; // y axis movement

When i is 0 ~ 3, it means "D", "L", "R", "U";
for example, when i = 0, x + 1, y + 0 means that the direction of the ordinate remains unchanged, and the direction of the abscissa moves downward , Which is "D"


The complete code is as follows:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = 500 + 10;
struct node{
    int x,y,h;
    string s;
    node(int a,int b,int c){
        x=a;
        y=b;
        h=c;
        s="";
    }
};
char map[maxn][maxn];
int vis[maxn][maxn];
int dr[] = {1,0,0,-1};
int dc[] = {0,-1,1,0};
string d[] = {"D","L","R","U"};
int n,m;
int h=0;
int hx,hy;
char res[maxn];
int cnt=0;
bool bfs(int x,int y){
    vis[x][y]=1;
    queue<node> q;
    node start = node(x,y,0);
    q.push(start);
    while(!q.empty()){
        node u = q.front();
        q.pop();
        int ux = u.x;
        int uy = u.y;
        int uh = u.h;
        string us = u.s;
        if(ux==n-1 && uy==m-1){  //结束条件 
            cout<<uh<<endl<<us;
            return true;
        }
        for(int i=0;i<=3;i++){
            int newx = ux + dr[i];
            int newy = uy + dc[i];
            int newh = uh + 1;
            if(newx<n && newx>=0 && newy<m && newy>=0
            && vis[newx][newy]==0
            && map[newx][newy]=='0'){
                vis[newx][newy]=1;
                node newn = node(newx,newy,newh);
                newn.s = us + d[i];
                q.push(newn);
            }
        }
    }
    return false;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        scanf("%s",&map[i]);
    }
    bfs(0,0);
    return 0;
}

 

 

Published 20 original articles · won 15 · views 217

Guess you like

Origin blog.csdn.net/qq_37414463/article/details/105376544