The title roughly describes:
The maze consists of n rows and m columns of cells (n, m are less than or equal to 50)
Each cell is either an open space or an obstacle
Find the shortest path length from the start to the end
1 / * 2 * @Descripttion: 3 * @version: 4 * @Author: ZKYAAA 5 * @Date: 2020-04-20 22:25:27 6 * @LastEditors: Please call me ZK 訕 啊啊 啊 7 * @ LastEditTime: 2020-04-20 23:42:48 8 9 * Problem description: 10 * The maze is composed of n rows and m columns of cells (n, m are less than or equal to 50) 11 * Each cell is either open or It is an obstacle 12 * Find the shortest path length from the start point to the end point 13 14 5 4 15 1 1 2 1 16 1 1 1 1 1 17 1 1 2 1 18 1 2 1 1 19 1 1 1 2 20 1 1 4 3 21 * / 22 23 #include <bits / stdc ++. H> 24 using namespace std; 25 const int MAXN = 100 ; 26 int p, q, Min = 1e9, n, m; 27 int a [MAXN] [MAXN]; // 1 Means open space, 2 means obstacle 28 int vis [MAXN] [MAXN]; // 0 means no visit, 1 means visit 29 30 int dir [ 4 ] [ 2 ] = { 31 { 0 , 1 }, 32 { 1 , 0 }, 33 { 0 , -1 }, 34 { -1 , 0 }, 35 }; 36 37 // int dirx [4] = {0,1,0, -1}; // The four directions are bottom left and top right 38 // int diry [4] = {1,0, -1,0}; 39 40 void dfs ( int x, int y, int step) { 41 if (x == p && y == q) { 42 if (step < Min ) { 43 Min = step; 44 } 45 return ; // backtrack 46 } 47 // clockwise lower right upper left trial 48 // optimized code 49 for ( int k = 0 ; k < 4 ; k ++ ) { 50 // int dx = x + dirx [k] ; 51 // int dy = y + diry [k]; 52 int dx = x + dir [k] [ 0 ]; 53 int dy = y + dir [k] [ 1 ]; 54 if (a [dx] [ dy] == 1 && vis [dx] [dy] == 0 ) { 55 vis [dx] [dy] =1 ; 56 dfs (dx, dy, step + 1 ); 57 vis [dx] [dy] = 0 ; 58 } 59 } 60 / * 61 // right 62 if (a [x] [y + 1] == 1 && vis [x] [y + 1] == 0) // Determine whether the right side of (x, y) is visited and is not an obstacle 63 { 64 vis [x] [y + 1] = 1; 65 dfs (x, y + 1, step + 1); 66 vis [x] [y + 1] = 0; // back to this point after dfs is executed, mark unvisited, continue to find step 67 } 68 // Next 69 if (a [x +1] [y] == 1 && vis [x + 1] [y] == 0) // Determine whether the bottom of (x, y) is visited and is not an obstacle 70 { 71 vis [x + 1] [y] = 1; 72 dfs (x + 1, y, step + 1); 73 vis [x + 1] [y] = 0; // dfs back to this after execution Point, mark not visited, continue to find step 74 } 75 // left 76 if (a [x-1] [y] == 1 && vis [x-1] [y] == 0) // judgment (x, y) Is the left side of the site visited and not an obstacle 77 { 78 vis [x-1] [y] = 1; 79 dfs (x-1, y, step + 1); 80 vis [x-1] [y] = 0 ; // dfs traces back to this point after execution, marks not visited, continue to find step 81 } 82 // up 83 if (a [x] [y-1] == 1 && vis [x] [y-1] == 0 ) // determine whether (x, y) and the top is not an obstacle to access 84 { 85 vis [x] [y-1] = 1; 86 dfs (x, y-1, step + 1); 87 vis [x] [y-1] = 0; // dfs back to this point after execution , Mark not visited, continue to find step 88 } 89 * / 90 return ; // back 91 } 92 93 int main () { 94 int startx, starty; 95 memset (vis, 0 , sizeof (vis)); 96 cin >> m >> n; 97 // At the beginning, it was written as n first and then m to create a labyrinth. It has been running with error 5 98 for (int i=1;i<=m;i++) 99 for(int j=1;j<=n;j++) 100 cin>>a[i][j]; 101 cin>>startx>>starty>>p>>q; 102 vis[startx][starty]=1; 103 dfs(startx,starty,0); 104 cout<<Min; 105 return 0; 106 }