DFS + BFS --- (recorriendo el laberinto) mapa bidimensional para encontrar el menor número de pasos

Descripción del título:

El paso más corto para encontrar la ruta del mapa de matriz bidimensional

(El subíndice comienza desde 1)

0 0 1 0

0 0 0 0

0 0 1 0

0 1 0 0

0 0 0 1

Tal matriz 4 * 5, 1 representa un obstáculo, 0 puede ir, comenzando desde (1,1), a la posición de (4,3) *** La respuesta debe ser 7. Hay dos formas de caminar .

  • Enfoque BFS: BFS utiliza un recorrido jerárquico, por lo que el número de pasos es el número más corto de pasos cuando se atraviesa hasta el final por primera vez.
    1 #include <bits / stdc ++. H>
     2  usando el  espacio de nombres std;
     3  struct Point {
     4    int x, y; // Coordinate 
    5    int step; // Registra el número de pasos desde el punto inicial a cada punto 
    6    Point ( int c, int d, int s): x (c), y (d), paso (s) {}
     7  };
     8  const  int maxn = 20 ;
     9  int start_x, start_y, end_x, end_y;
     10  int m, n, cnt ;
     11  bool inqueue [maxn] [maxn] = { false};
     12  int map_ [maxn] [maxn] = { 0 };
     13  int next [ 4 ] [ 2 ] = { 0 , 1 , 0 , -1 , 1 , 0 , -1 , 0 };
     14  BFS vacío ( int x, int y) {
     15      cola <Point> Q;
     16      Q.push (Point (x, y, 0 )); // presione el punto inicial 
    17      inqueue [x] [y] = verdadero ; // marque Sobre equipo
    18      while (! Q.empty ()) {
     19          Point top = Q.front ();
    20          Q.pop ();
    21          if (top.x == end_x && top.y == end_y) { // 边界
    22              cnt = top.step;
    23              regreso ;
    24          }
     25          para ( int i = 0 ; i < 4 ; i ++ ) {
     26              int tx = top.x + siguiente [i] [ 0 ];
    27              int ty = top.y + siguiente [i] [ 1 ];
    28              if (tx < 1|| ty < 1 || tx> m || ty> n || inqueue [tx] [ty] == verdadero || map_ [tx] [ty] == 1 )
     29                  continuar ; // 剪枝
    30              inqueue [tx] [ty] = true ;
    31              Punto medio = Punto (tx, ty, top.step + 1 );
    32              Q.push (mediados);
    33          }
     34      }
     35  }
     36  int main () {
     37      while (cin >> m >> n) {
     38          cnt = 0 ;
    39          memset (inqueue, false ,sizeof (inqueue));
    40          memset (map_, false , sizeof (map_));
    41          para ( int i = 1 ; i <= m; i ++ ) {
     42              para ( int j = 1 ; j <= n; j ++ ) {
     43                  cin >> map_ [i] [j];
    44              }
     45          }
     46          cin >> inicio_x >> inicio_y >> fin_x >> fin_y;
    47          Punto de inicio = Punto (inicio_x, inicio_y, 0 ); // 初始化 起始点
    48          BFS (start.x, start.y);
    49          cout << cnt << endl;
    50      }
     51      devuelve  0 ;
    52 }

    Prácticas DFS

    1 #include <bits / stdc ++. H>
     2  usando el  espacio de nombres std;
    3  int m, n, start_x, start_y, end_x, end_y, cnt, min_step = 1000 ;
    4  const  int maxn = 20 ;
    5  int mapa_ [maxn] [maxn];
    6  visita de bool [maxn] [maxn];
    7  int siguiente [ 4 ] [ 2 ] = { 0 , 1 , 0 , - 1 , 1 , 0 , - 1 , 0 };
    8  nuloDFS ( int x, int y, int step) {
     9      if (x == end_x && y == end_y) {
     10          if (step < min_step)
     11              min_step = step;
    12          regreso ;
    13      }
     14      para ( int i = 0 ; i < 4 ; i ++ ) {
     15          int tx = x + siguiente [i] [ 0 ];
    16          int ty = y + siguiente [i] [ 1 ];
    17          if (tx < 1 || ty < 1|| tx> m || ty> n || visita [tx] [ty] == verdadero || map_ [tx] [ty] == 1 ) continuar ;
    18          visita [tx] [ty] = verdadero ;
    19          DFS (tx, ty, paso + 1 );
    20          visita [tx] [ty] = falso ;
    21      }
     22  }
     23  int main () {
     24      while (cin >> m >> n) {
     25          min_step = 1000 ;
    26          memset (visita, falso , tamaño de (visita));
    27          memset (mapa_, 0, sizeof (visita));
    28          para ( int i = 1 ; i <= m; i ++ ) {
     29              para ( int j = 1 ; j <= n; j ++ ) {
     30                  cin >> map_ [i] [j];
    31              }
     32          }
     33          cin >> inicio_x >> inicio_y >> fin_x >> fin_y;
    34          visita [inicio_x] [inicio_y] = verdadero ;
    35          DFS (inicio_x, inicio_y, 0 );
    36  
    37          cout << min_step << endl;
    38      }
    39      devuelven  0 ;
    40 }

     

 

Supongo que te gusta

Origin www.cnblogs.com/YanShaoDian/p/12686880.html
Recomendado
Clasificación