Advertisement
NightFox

Untitled

Nov 16th, 2020
911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.21 KB | None | 0 0
  1.  
  2. /*** Pathfindig ***/
  3. int8_t Demo::PathFinding(int8_t sx, int8_t sy, int8_t ex, int8_t ey) {
  4.  
  5.     // Proteccion de coordenadas
  6.     if ((sx < 0) || (ex < 0) || (sx >= maze_w) || (ex >= maze_w)) return -1;
  7.     if ((sy < 0) || (ey < 0) || (sy >= maze_h) || (ey >= maze_h)) return -1;
  8.  
  9.     // Si la entrada o salida es invalida
  10.     if ((maze_shape[sy][sx] != maze_floor) || (maze_shape[ey][ex] != maze_floor)) return -1;
  11.  
  12.     // Marca la salida del laberinto
  13.     maze_dumbbells[ey][ex] = maze_target;
  14.  
  15.     // Genera la red de pesos
  16.     MazeDumbbells(sy, sx, 1);
  17.  
  18.     // Marca la salida del laberinto
  19.     maze_dumbbells[ey][ex] = maze_target;
  20.  
  21.     // Segun la red de pesos creada, busca la ruta mas corta
  22.     MazePath(sy, sx, 1);
  23.  
  24.     // Marca las entradas y salidas
  25.     maze_path[sy][sx] = maze_start;
  26.     maze_path[ey][ex] = maze_end;
  27.  
  28.     return 0;
  29.  
  30. }
  31.  
  32.  
  33.  
  34. /*** Busca la ruta a la salida - Colocacion de pesos ***/
  35. void Demo::MazeDumbbells(int8_t py, int8_t px, int32_t cnt) {
  36.  
  37.     // Si estas fuera de rango, devuelve -1
  38.     if ((py < 0) || (px < 0) || (py >= maze_h) || (px >= maze_w)) return;
  39.  
  40.     // Exploracion previa
  41.     if (maze_dumbbells[py][px] == maze_target) {                                    // Si es la salida, sal
  42.         if (path_max_deep == maze_invalid) {
  43.             path_max_deep = cnt;
  44.         } else {
  45.             if (cnt < path_max_deep) path_max_deep = cnt;
  46.         }
  47.         return;
  48.     } else if (maze_dumbbells[py][px] == maze_invalid) {                            // Si esta marcada como invalida, sal
  49.         return;
  50.     } else if ((maze_dumbbells[py][px] != 0) && (maze_dumbbells[py][px] < cnt)) {   // Si ya esta visitada y pesa menos, sal
  51.         return;
  52.     }
  53.  
  54.     // Coloca el peso en la casilla si es inferior
  55.     maze_dumbbells[py][px] = cnt;
  56.  
  57.     // Incrementa en 1 el peso
  58.     cnt ++;
  59.  
  60.     // Recursividad en 4 direcciones
  61.     MazeDumbbells((py - 1), px, cnt);
  62.     MazeDumbbells((py + 1), px, cnt);
  63.     MazeDumbbells(py, (px - 1), cnt);
  64.     MazeDumbbells(py, (px + 1), cnt);
  65.  
  66. }
  67.  
  68.  
  69.  
  70. /*** Busca la ruta a la salida - Ruta mas corta ***/
  71. int8_t Demo::MazePath(int8_t py, int8_t px, int32_t cnt) {
  72.  
  73.     // Si estas fuera de rango, devuelve -1
  74.     if ((py < 0) || (px < 0) || (py >= maze_h) || (px >= maze_w)) return maze_invalid;
  75.  
  76.     // Exploracion previa
  77.     if (maze_dumbbells[py][px] == maze_target) {                // Si es la salida, sal
  78.         return maze_target;
  79.     } else if (maze_dumbbells[py][px] == maze_invalid) {        // Si esta marcada como invalida, sal
  80.         return maze_invalid;
  81.     } else if (maze_dumbbells[py][px] != cnt) {                 // Si el peso no coincide, sal
  82.         return maze_invalid;
  83.     } else if (cnt > path_max_deep) {
  84.         return maze_invalid;
  85.     }
  86.  
  87.     // Incrementa en 1 el peso
  88.     cnt ++;
  89.  
  90.     // Recursividad en 4 direcciones
  91.     if (
  92.         (MazePath((py - 1), px, cnt) == maze_target)
  93.         ||
  94.         (MazePath((py + 1), px, cnt) == maze_target)
  95.         ||
  96.         (MazePath(py, (px - 1), cnt) == maze_target)
  97.         ||
  98.         (MazePath(py, (px + 1), cnt) == maze_target)
  99.     ) {
  100.         maze_path[py][px] = maze_target;
  101.         return maze_target;
  102.     }
  103.  
  104.     return maze_invalid;
  105.  
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement