Created
April 6, 2020 02:44
-
-
Save bwedding/9fb072c726508d9a049f725aac1d31f1 to your computer and use it in GitHub Desktop.
Solve a maze by dead end filling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// DeadEndMazeSolver.cpp : This file contains the 'main' function. Program execution begins and ends there. | |
// | |
#include <iostream> | |
#include <vector> | |
#include <thread> | |
#include <chrono> | |
using Maze = std::vector<std::vector<bool>>; | |
using PrintableMaze = std::vector<std::vector<unsigned char>>; | |
const unsigned char EndChar = 157; | |
const unsigned char StartChar = '+'; | |
const unsigned char Solid = 219; | |
const unsigned char Opened = 176; | |
struct Position | |
{ | |
int x, y; | |
}; | |
void DrawMaze(PrintableMaze& maze) | |
{ | |
std::cout << "\n"; | |
for (const auto& row : maze) | |
{ | |
for (const auto& ch : row) | |
{ | |
std::cout << (unsigned char)ch; | |
} | |
std::cout << "\n"; | |
} | |
} | |
bool IsEnd(Position &curPos, const PrintableMaze &maze) | |
{ | |
int routes = 0; | |
// Check its left route | |
if (curPos.y > 0 | |
&& maze.at(curPos.x).at(curPos.y-1) != Solid) | |
routes++; | |
// Check its right route | |
if (curPos.y < maze.at(curPos.x).size()-1 && | |
maze.at(curPos.x).at(curPos.y+1) != Solid) | |
routes++; | |
// Check its upper route | |
if (curPos.x > 0 && | |
maze.at(curPos.x-1).at(curPos.y) != Solid) | |
routes++; | |
// Check its lower route | |
int sz = maze.size(); | |
if (curPos.x < maze.size()-1 && | |
curPos.y < maze.at(curPos.x).size()-1 && | |
maze.at(curPos.x + 1).at(curPos.y) != Solid) | |
routes++; | |
if (routes < 2) | |
return true; | |
else | |
return false; | |
} | |
bool MarkDeadEnds(PrintableMaze& maze) | |
{ | |
int x = 0, y = 0; | |
bool found = false; | |
for (const auto& row : maze) | |
{ | |
for (const auto& ch : row) | |
{ | |
// If it's start or end, continue | |
if (ch == StartChar || ch == EndChar || ch == Solid) | |
{ | |
y++; | |
continue; | |
} | |
Position curPos = { x, y }; | |
if(IsEnd(curPos, maze)) | |
{ | |
maze[x][y] = Solid; | |
found = true; | |
} | |
y++; | |
} | |
x++; y = 0; | |
} | |
return found; | |
} | |
PrintableMaze MakePrintableMaze(Maze& maze, Position& Start, Position& End, int width, int height) | |
{ | |
int x = 0, y = 0; | |
PrintableMaze pMaze(width, std::vector<unsigned char>(height)); | |
for (const auto& row : maze) | |
{ | |
for (const auto& ch : row) | |
{ | |
unsigned int block; | |
if (x == Start.x && y == Start.y) | |
block = StartChar; | |
else if (x == End.x && y == End.y) | |
block = EndChar; | |
else | |
block = ch ? Opened : Solid; | |
pMaze[x][y] = block; | |
y++; | |
} | |
x++; | |
y = 0; | |
} | |
return pMaze; | |
} | |
int main() | |
{ | |
Maze maze = { | |
{ 1, 0, 1, 1, 0 }, | |
{ 1, 1, 1, 0, 1 }, | |
{ 0, 1, 0, 1, 0 }, | |
{ 1, 1, 1, 1, 1 } | |
}; | |
Position Start = { 0.0 }; | |
Position End = { 2,3 }; | |
// Make printable maze | |
PrintableMaze pMaze = MakePrintableMaze(maze, Start, End, maze.size(), maze[0].size()); | |
// Draw Maze | |
do | |
{ | |
DrawMaze(pMaze); | |
std::this_thread::sleep_for(std::chrono::milliseconds(500)); | |
} while (MarkDeadEnds(pMaze)); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment