Files
.github
api-server
client
config
curriculum
docs
guide
arabic
chinese
english
3d
accessibility
agile
algorithms
algorithm-design-patterns
algorithm-performance
avl-trees
b-trees
backtracking-algorithms
binary-search-trees
boundary-fill
brute-force-algorithms
divide-and-conquer-algorithms
embarassingly-parallel-algorithms
evaluating-polynomials-direct-analysis
evaluating-polynomials-synthetic-division
exponentiation
flood-fill
graph-algorithms
greatest-common-divisor-direct-analysis
greatest-common-divisor-euclidean
greedy-algorithms
lee-algorithm
index.md
quickselection-algorithm
red-black-trees
search-algorithms
sorting-algorithms
string-matching-algorithms
index.md
android-development
angular
angularjs
apache
aspnet
bash
blender
blockchain
bootstrap
bsd-os
bulma
c
canvas
certifications
chef
clojure
cloud-development
codeigniter
computational-genomics
computer-hardware
computer-science
containers
cplusplus
csharp
css
d3
data-science-tools
design-patterns
designer-tools
developer-ethics
developer-tools
devops
docker
documentation
drupal
electron
elixir
elm
erlang
fsharp
game-development
gatsbyjs
git
go
groovy
haskell
haxe
hibernate
html
ionic
java
javascript
joomla
jquery
julia
kotlin
laravel
linux
logic
machine-learning
mathematics
meta
miscellaneous
mobile-app-development
mongodb
natural-language-processing
neovim
network-engineering
nginx
nodejs
optical-alignment
php
product-design
progressive-web-apps
puppet
python
r
raspberry-pi
react
react-native
redux
rest-api
robotics
rt-os
ruby
rust
sass
security
semantic-ui
software-engineering
sql
ssh
svg
svn
swift
terminal-commandline
tomcat
tools
typescript
typography
user-experience-design
user-experience-research
vagrant
vim
virtualbox
visual-design
voice
vue
vue-cli
web-augmented-reality
web-components
web-performance
web-virtual-reality
wordpress
working-in-tech
xml
portuguese
russian
spanish
mock-guide
tools
.editorconfig
.eslintignore
.eslintrc
.gitattributes
.gitignore
.node-inspectorrc
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE.md
README(french).md
README-IT
README.md
docker-compose-shared.yml
docker-compose.yml
lerna.json
netlify.toml
package-lock.json
package.json
sample.env
freeCodeCamp/guide/english/algorithms/lee-algorithm/index.md

61 lines
1.8 KiB
Markdown
Raw Normal View History

2018-10-12 15:37:13 -04:00
---
title: Lee's Algorithm
---
## Lee's Algorithm
The Lee algorithm is one possible solution for maze routing problems. It always gives an optimal solution, if one exists, but is
slow and requires large memory for dense layout.
### Understanding how it works
The algorithm is a `breadth-first` based algorithm that uses `queues` to store the steps. It usually uses the following steps:
1. Choose a starting point and add it to the queue.
2. Add the valid neighboring cells to the queue.
3. Remove the position you are on from the queue and continue to the next element.
4. Repeat steps 2 and 3 until the queue is empty.
### Implementation
C++ has the queue already implemented in the `<queue>` library, but if you are using something else you are welcome to implement
your own version of queue.
C++ code:
```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
2018-10-12 15:37:13 -04:00
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();
2018-10-12 15:37:13 -04:00
}
}
```