Files
freeCodeCamp/guide/spanish/algorithms/lee-algorithm/index.md
Randell Dawson 0a1eeea424 fix(guide) Replace invalid prism code block names (#35961)
* fix: replace sh with shell

fix replace terminal with shell

fix replace node with js

fix replace output with shell

fix replace cs with csharp

fix replace c++ with cpp

fix replace c# with csharp

fix replace javasctipt with js

fix replace syntax  with js

fix replace unix with shell

fix replace linux with shell

fix replace java 8 with java

fix replace swift4 with swift

fix replace react.js with jsx

fix replace javascriot with js

fix replace javacsript with js

fix replace c++ -  with cpp

fix: corrected various typos

fix: replace Algorithm with nothing

fix: replace xaml with xml

fix: replace solidity with nothing

fix: replace c++ with cpp

fix: replace txt with shell

fix: replace code with json and css

fix: replace console with shell
2019-05-15 19:08:19 +02:00

1.9 KiB

title, localeTitle
title localeTitle
Lee's Algorithm Algoritmo de Lee

Algoritmo de Lee

El algoritmo de Lee es una solución posible para problemas de enrutamiento de laberinto. Siempre da una solución óptima, si existe, pero es Lento y requiere gran memoria para un diseño denso.

Entendiendo como funciona

El algoritmo es un algoritmo basado en breadth-first que usa queues para almacenar los pasos. Por lo general, utiliza los siguientes pasos:

  1. Elija un punto de partida y agréguelo a la cola.
  2. Agregue las celdas vecinas válidas a la cola.
  3. Elimine la posición en la que se encuentra de la cola y continúe con el siguiente elemento.
  4. Repita los pasos 2 y 3 hasta que la cola esté vacía.

Implementación

C ++ ya tiene la cola implementada en la biblioteca <queue> , pero si está utilizando algo más, puede implementar tu propia versión de la cola.

Código C ++:

int dl[] = {-1, 0, 1, 0}; // these arrays will help you travel in the 4 directions more easily 
 int dc[] = {0, 1, 0, -1}; 
 
 queue<int> X, Y; // the queues used to get the positions in the matrix 
 
 X.push(start_x); //initialize the queues with the start position 
 Y.push(start_y); 
 
 void lee() 
 { 
  int x, y, xx, yy; 
  while(!X.empty()) // while there are still positions in the queue 
  { 
    x = X.front(); // set the current position 
    y = Y.front(); 
    for(int i = 0; i < 4; i++) 
    { 
      xx = x + dl[i]; // travel in an adiacent cell from the current position 
      yy = y + dc[i]; 
      if('position is valid') //here you should insert whatever conditions should apply for your position (xx, yy) 
      { 
          X.push(xx); // add the position to the queue 
          Y.push(yy); 
          mat[xx][yy] = -1; // you usually mark that you have been to this position in the matrix 
      } 
 
    } 
 
    X.pop(); // eliminate the first position, as you have no more use for it 
    Y.pop(); 
 
  } 
 
 
 }