#include<iostream>
#include<stack>
#include<vector>
using namespace std;template<class T>
class Maze {
public:Maze( //默认参数值pair<int, int> initSize = make_pair(15, 17),pair<string, string> initStyle = make_pair("█", "□"), vector<string> initPathStyle = { string("⊙") },//☆⊙pair<int, int> initEn = make_pair(1, 1)) : //初始化maze_size(initSize),maze_style(initStyle),maze_path_style(initPathStyle),maze_entrance(initEn),maze_export(initSize),maze_direction({{0,1},{1,0},{0,-1},{-1,0}}){runMaze();}private:pair<int, int> maze_size; //迷宫大小pair<string, string> maze_style; //迷宫样式vector<string> maze_path_style; //路径样式pair<int, int> maze_entrance; //迷宫入口pair<int, int> maze_export; //迷宫出口vector<vector<int>> maze_vector; //迷宫数组stack<pair<int, int>> maze_path; //迷宫路径vector<pair<int, int>> maze_direction; //寻径方向vector<vector<int>> maze_random_vector; //随机生成的有效迷宫vector<pair<int, int>> maze_path_point; //迷宫路径点坐标集合void initMaze(); //初始化迷宫数据bool findPath(); //寻找成功的路径void createMazeMap(); //创建迷宫地图void delay(double t = 0.1); //延迟函数void outputMazeMap(); //输出迷宫地图void runMaze(); //运行迷宫
};template<class T>
void Maze<T>::initMaze() {vector<int> temp;maze_vector.clear();maze_random_vector.clear();maze_path_point.clear();for (int i = 0; i <= maze_size.first + 1; i++) {for (int j = 0; j <= maze_size.second + 1; j++) {if (i == 0 || i == maze_size.first + 1 || j == 0 || j == maze_size.second + 1) {temp.push_back(1);}else if (make_pair(i, j) == maze_entrance || make_pair(i, j) == maze_export) {temp.push_back(0);} else {temp.push_back(rand() % 2);}}maze_vector.push_back(temp);temp.clear();}maze_random_vector = maze_vector;
}template<class T>
bool Maze<T>::findPath() {pair<int, int> current_location = maze_entrance; //当前位置等于迷宫入口maze_vector.at(1).at(1) = 1; //标记入口值为1int selected_direction = 0; //当前执行的方向int other_direction = 3; //剩余其他方向while (current_location != maze_export) {int new_row, new_col;while (selected_direction <= other_direction) {new_row = current_location.first + maze_direction.at(selected_direction).first;new_col = current_location.second + maze_direction.at(selected_direction).second;if (maze_vector.at(new_row).at(new_col) == 0)break;selected_direction++;}if (selected_direction <= other_direction) {maze_path.push(current_location);current_location.first = new_row;current_location.second = new_col;maze_vector.at(new_row).at(new_col) = 1;selected_direction = 0;}else {if (maze_path.empty())return false;pair<int, int> temp = maze_path.top();maze_path.pop();if (temp.first == current_location.first)selected_direction = 2 + temp.second - current_location.second;elseselected_direction = 3 + temp.first - current_location.first;current_location = temp;}}maze_path.push(current_location);return true;
}template<class T>
void Maze<T>::createMazeMap() {//随机生成迷宫地图srand(time(0));initMaze();while (!findPath()) {cout << "迷宫生成中..." << endl;system("cls");initMaze();}//将迷宫路径点导入到vectorwhile (!maze_path.empty()) {maze_path_point.push_back(maze_path.top());maze_path.pop();}
}template<class T>
void Maze<T>::delay(double t) {clock_t start_time;start_time = clock();while ((clock() - start_time) < t * CLOCKS_PER_SEC) {}
}template<class T>
void Maze<T>::outputMazeMap() {//反序输出vector<pair<int, int>>::reverse_iterator it = maze_path_point.rbegin();for (; it != maze_path_point.rend(); it++) {maze_random_vector.at(it->first).at(it->second) = 3;for (auto i : maze_random_vector) {for (auto j : i) {if (!j)cout << maze_style.first;elseif (j == 1)cout << maze_style.second;elsecout << maze_path_style.at(0);}cout << endl;}if (*it == maze_export)system("pause");delay(0.155);system("cls");}
}template<class T>
void Maze<T>::runMaze() {createMazeMap();outputMazeMap();
}int main() {Maze<int> m;system("pause");return 0;
}
运行后效果