diff --git a/lee_solveMaze.cpp b/lee_solveMaze.cpp new file mode 100644 index 0000000..b27b7b5 --- /dev/null +++ b/lee_solveMaze.cpp @@ -0,0 +1,105 @@ +//g++ -Wall --std=c++11 +#include +#include +#include +#include + +#define MAZE_SIZE 100 + +typedef std::array, MAZE_SIZE> maze; +typedef std::pair point; + +int dLine[] = {-1, 1, 0, 0}; +int dColumn[] = { 0, 0, 1, -1}; + +maze leeSolveMaze(const maze &initial, int size, const point &entry, const point &exit){ + maze r; + + //initialize the result matrix so every step can be optimized at first + int maxSteps = size*size + 1; + for(int i=1; i<=size; i++){ + for(int j=1; j<=size; j++){ + r[i][j] = maxSteps; + } + } + + std::queue q; + point current; + + q.push(entry); + r[entry.first][entry.second]=1; + while(!q.empty()){ + int nextLine, nextCol; + current = q.front(); + q.pop(); + + for(int i=0; i<4; i++){ + nextLine = current.first + dLine[i]; + nextCol = current.second + dColumn[i]; + if(r[nextLine][nextCol] > r[current.first][current.second] && + initial[nextLine][nextCol] != 1){ + r[nextLine][nextCol] = r[current.first][current.second] + 1; + q.push(std::make_pair(nextLine, nextCol)); + } + + if(nextLine == exit.first && nextCol == exit.second){ + return r; + } + } + } + + return r; +} + +void displayPath(const maze &solvedMaze, const point &entry, const point &exit){ + int currentLine = exit.first; + int currentCol = exit.second; + int nextLine, nextCol; + + if(currentLine != entry.first || currentCol != entry.second){ + for(int i=0; i<4; i++){ + nextLine = currentLine + dLine[i]; + nextCol = currentCol + dColumn[i]; + + if(solvedMaze[nextLine][nextCol] == solvedMaze[currentLine][currentCol]-1){ + displayPath(solvedMaze, entry, std::make_pair(nextLine, nextCol)); + std::cout<, 100> solvedMaze, initial = + {{ + {{1,1,1,1,1,1,1}}, + {{1,0,1,0,0,0,1}}, + {{1,0,1,1,1,0,1}}, + {{1,0,0,0,1,0,1}}, + {{1,0,1,0,1,0,1}}, + {{1,0,0,0,0,0,1}}, + {{1,1,1,1,1,1,1}} + }}; + + point entry = std::make_pair(1, 1); + point exit = std::make_pair(1, 3); + + solvedMaze = leeSolveMaze(initial, size, entry, exit); + + for(int i=1; i<=size; i++){ + for(int j=1; j<=size; j++){ + std::cout.width(2); + std::cout<