Skip to content

Commit b161b5e

Browse files
committed
solving a maze using Lee's algorithm
1 parent 40899d8 commit b161b5e

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

lee_solveMaze.cpp

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//g++ -Wall --std=c++11
2+
#include <queue>
3+
#include <iostream>
4+
#include <array>
5+
#include <utility>
6+
7+
#define MAZE_SIZE 100
8+
9+
typedef std::array<std::array<int, MAZE_SIZE>, MAZE_SIZE> maze;
10+
typedef std::pair<int, int> point;
11+
12+
int dLine[] = {-1, 1, 0, 0};
13+
int dColumn[] = { 0, 0, 1, -1};
14+
15+
maze leeSolveMaze(const maze &initial, int size, const point &entry, const point &exit){
16+
maze r;
17+
18+
//initialize the result matrix so every step can be optimized at first
19+
int maxSteps = size*size + 1;
20+
for(int i=1; i<=size; i++){
21+
for(int j=1; j<=size; j++){
22+
r[i][j] = maxSteps;
23+
}
24+
}
25+
26+
std::queue<point> q;
27+
point current;
28+
29+
q.push(entry);
30+
r[entry.first][entry.second]=1;
31+
while(!q.empty()){
32+
int nextLine, nextCol;
33+
current = q.front();
34+
q.pop();
35+
36+
for(int i=0; i<4; i++){
37+
nextLine = current.first + dLine[i];
38+
nextCol = current.second + dColumn[i];
39+
if(r[nextLine][nextCol] > r[current.first][current.second] &&
40+
initial[nextLine][nextCol] != 1){
41+
r[nextLine][nextCol] = r[current.first][current.second] + 1;
42+
q.push(std::make_pair(nextLine, nextCol));
43+
}
44+
45+
if(nextLine == exit.first && nextCol == exit.second){
46+
return r;
47+
}
48+
}
49+
}
50+
51+
return r;
52+
}
53+
54+
void displayPath(const maze &solvedMaze, const point &entry, const point &exit){
55+
int currentLine = exit.first;
56+
int currentCol = exit.second;
57+
int nextLine, nextCol;
58+
59+
if(currentLine != entry.first || currentCol != entry.second){
60+
for(int i=0; i<4; i++){
61+
nextLine = currentLine + dLine[i];
62+
nextCol = currentCol + dColumn[i];
63+
64+
if(solvedMaze[nextLine][nextCol] == solvedMaze[currentLine][currentCol]-1){
65+
displayPath(solvedMaze, entry, std::make_pair(nextLine, nextCol));
66+
std::cout<<currentLine<<" "<<currentCol<<"\n";
67+
return; //don't continue to go on all possible paths, one is enough
68+
}
69+
}
70+
}
71+
else{
72+
std::cout<<entry.first<<" "<<entry.second<<"\n";
73+
}
74+
}
75+
76+
int main(){
77+
int size = 5; //the maze is 5x5
78+
//the array is bordered with obstacles so we don't get out of it
79+
std::array<std::array<int, 100>, 100> solvedMaze, initial =
80+
{{
81+
{{1,1,1,1,1,1,1}},
82+
{{1,0,1,0,0,0,1}},
83+
{{1,0,1,1,1,0,1}},
84+
{{1,0,0,0,1,0,1}},
85+
{{1,0,1,0,1,0,1}},
86+
{{1,0,0,0,0,0,1}},
87+
{{1,1,1,1,1,1,1}}
88+
}};
89+
90+
point entry = std::make_pair(1, 1);
91+
point exit = std::make_pair(1, 3);
92+
93+
solvedMaze = leeSolveMaze(initial, size, entry, exit);
94+
95+
for(int i=1; i<=size; i++){
96+
for(int j=1; j<=size; j++){
97+
std::cout.width(2);
98+
std::cout<<solvedMaze[i][j]<< " ";
99+
}
100+
std::cout<<"\n";
101+
}
102+
103+
displayPath(solvedMaze, entry, exit);
104+
}
105+

0 commit comments

Comments
 (0)