Advanced Maze

Foreword: The processing of the maze DFS. Mark it for easy review later.


Advanced Maze

Tip: The template for walking the maze was actually published on the blog yesterday. Today's is an advanced process. If you want to see yesterday's template, click here —> Snake .


1. Set the problem-advanced maze

After thinking, Mr. Suantou finally solved the problem of how to calculate the shortest path of a maze, so Mr. Suantou found a new maze map to verify whether he could really calculate the shortest path of a maze.

In order to check whether your calculations are correct, Mr. Garlic invites you to calculate together.

Input format
Input two integers nn and mm in the first line, which means that this is an n×m maze.

Next, enter a maze with nn rows and mm columns. Among them,'@' represents the location of the garlic head,'#' represents the wall, and the garlic head cannot pass through, and the'.' represents the road, the garlic head can move through the'.', and all the'.' in the outermost part of the maze represent the exit of the maze (Jun Suantou can only move to four positions adjacent to him at a time-up, down, left, and right).

Output format
Output an integer, which represents the minimum number of steps for Mr. Suantou to escape the maze. If Mr. Suantou cannot escape the maze, output -1−1.

Data range
1 \le n,m \le 151≤n,m≤15.

The extra spaces at the end of each line during output will not affect the correctness of the answer

样例输入1
9 13
#############
#@..........#
#####.#.#.#.#
#...........#
#.#.#.#.#.#.#
#.#.......#.#
#.#.#.#.#.#.#
#...........#

样例输出1
11
样例输入2
4 6
#.####
#.#.##
#...@#
######
样例输出2
5

2. Ideas

In fact, I was very excited to see this topic. It is estimated that I only need to use a DFS or BFS template to solve it. Later, I read the question carefully and found that this is to get out of the maze, that is to say, the pop-up condition is to walk into the wall, and the wall It is a'.', this is the first difference, the second is to output the shortest number of steps, so you will see that the dis array is introduced behind to help solve this problem

1. Preliminary preparation

const int INF = 0x3f3f3f3f;
const int maxn = 1005;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
//这里其实可以建一个pair来解决,当然结构体适合初学者使用,重点就是要存储每个节点的x,y的坐标
typedef struct node{
    int x,y;
}P;
int n,m;
int xs,ys;
char mp[maxn][maxn];
int dis[maxn][maxn];
int ans;

2.dfs() module display

The code is as follows (example):

int bfs()
{
    
    
	//初始化将每个点的距离设置为无穷大
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            dis[i][j]=INF;
	
    queue<P>que;
    que.push(P{
    
    xs,ys});
    dis[xs][ys] = 0;
    //确保que在未空之前都能把里面的数据跑完
    while(!que.empty())
    {
    
    
        P p = que.front();
        que.pop();
        //跑四个情况,上下左右
        for(int i=0;i<4;i++)
        {
    
    
            int h = p.x + dx[i];
            int l = p.y + dy[i];
            if(h>=0&&l>=0&&h<n&&l<m&&mp[h][l]!='#'&&dis[h][l]==INF)
            {
    
    
                que.push(P{
    
    h,l});
                //距离在原来的基础上+1,也就是走了一步
                dis[h][l] = dis[p.x][p.y] + 1,这样输出的时候我们只用输出最后一个
            }
            //判断弹出条件,走到墙里头了(奇怪的比喻)
            if(mp[h][l]=='.'&&(h==0||l==0||h==n-1||l==m-1)) {
    
    
                ans = dis[h][l];
                return ans;
            }
        }

    }
    //如果在que等于空了以后还没跳出说明走不到终点,也就是找不到出路,那就直接返回INF
    return INF;

}

3.AC code

The code is as follows (example):

#include<bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1005;
int dx[]={
    
    1,0,-1,0},dy[]={
    
    0,1,0,-1};
typedef struct node{
    
    
    int x,y;
}P;
int n,m;
int xs,ys;
char mp[maxn][maxn];
int dis[maxn][maxn];
int ans;
int bfs()
{
    
    
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            dis[i][j]=INF;

    queue<P>que;
    que.push(P{
    
    xs,ys});
    dis[xs][ys] = 0;
    while(!que.empty())
    {
    
    
        P p = que.front();
        que.pop();
        for(int i=0;i<4;i++)
        {
    
    
            int h = p.x + dx[i];
            int l = p.y + dy[i];
            if(h>=0&&l>=0&&h<n&&l<m&&mp[h][l]!='#'&&dis[h][l]==INF)
            {
    
    
                que.push(P{
    
    h,l});
                dis[h][l] = dis[p.x][p.y] + 1;
            }
            //判断弹出条件
            if(mp[h][l]=='.'&&(h==0||l==0||h==n-1||l==m-1)) {
    
    
                ans = dis[h][l];
                return ans;
            }
        }

    }
    return INF;

}
int main(){
    
    
    while(~scanf("%d%d",&n,&m))
    {
    
    
        for(int i=0;i<n;i++)
           scanf("%s",mp[i]);

		//寻找起点
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                if(mp[i][j]=='@')
                {
    
    
                    xs = i;
                    ys = j;
                    break;
                }

        if(bfs()>=INF)cout << "-1" << endl;
        else cout << bfs() << endl;
    }
    return 0;
}


to sum up

All in all, in fact, search questions such as bfs have their own characteristics, but they are inseparable. Here is a summary of the usage: 1. Save the data 2. Find the starting point 3. Set the bfs 4. Push the initial data into the queue 5. Start the queue loop, set End condition 6. Launch

Guess you like

Origin blog.csdn.net/DAVID3A/article/details/114096437