迷宫问题数据结构c++,要求实现一下几个功能:添加显示迷宫,求解一条迷宫路径,求解所有迷宫路径,求解最短迷宫路径,给出完整代码
时间: 2024-02-26 21:08:48 浏览: 23
好的,以下是一个基于深度优先搜索算法的迷宫问题解法的代码实现,包括添加显示迷宫、求解一条迷宫路径、求解所有迷宫路径、求解最短迷宫路径等功能:
```c++
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
// 定义迷宫结构体
struct Maze {
int row; // 迷宫行数
int col; // 迷宫列数
vector<vector<int>> maze; // 二维迷宫数组
};
// 定义坐标结构体
struct Pos {
int x;
int y;
};
// 添加迷宫函数
void AddMaze(Maze& maze) {
cout << "请输入迷宫的行数和列数: ";
cin >> maze.row >> maze.col;
// 初始化迷宫为全部为墙
vector<vector<int>> mz(maze.row, vector<int>(maze.col, -1));
maze.maze = mz;
// 添加迷宫地图
cout << "请输入迷宫地图(0代表通路,1代表障碍): " << endl;
for (int i = 0; i < maze.row; i++) {
for (int j = 0; j < maze.col; j++) {
cin >> maze.maze[i][j];
}
}
}
// 显示迷宫函数
void ShowMaze(Maze& maze) {
cout << "当前迷宫地图如下: " << endl;
for (int i = 0; i < maze.row; i++) {
for (int j = 0; j < maze.col; j++) {
cout << maze.maze[i][j] << " ";
}
cout << endl;
}
}
// 判断当前位置是否合法
bool IsValidPos(Maze& maze, Pos& pos) {
if (pos.x < 0 || pos.x >= maze.row) {
return false;
}
if (pos.y < 0 || pos.y >= maze.col) {
return false;
}
if (maze.maze[pos.x][pos.y] == 1) {
return false;
}
return true;
}
// 深度优先搜索求解一条迷宫路径
bool DFS(Maze& maze, Pos& start, Pos& end, vector<Pos>& path, vector<vector<int>>& visited) {
// 如果当前位置为终点,则返回true
if (start.x == end.x && start.y == end.y) {
path.push_back(start);
return true;
}
// 如果当前位置不合法,则返回false
if (!IsValidPos(maze, start)) {
return false;
}
// 如果当前位置已被访问过,则返回false
if (visited[start.x][start.y] == 1) {
return false;
}
// 将当前位置标记为已访问
visited[start.x][start.y] = 1;
// 将当前位置添加到路径中
path.push_back(start);
// 分别向上、下、左、右四个方向搜索
Pos next;
next.x = start.x - 1;
next.y = start.y;
if (DFS(maze, next, end, path, visited)) {
return true;
}
next.x = start.x + 1;
next.y = start.y;
if (DFS(maze, next, end, path, visited)) {
return true;
}
next.x = start.x;
next.y = start.y - 1;
if (DFS(maze, next, end, path, visited)) {
return true;
}
next.x = start.x;
next.y = start.y + 1;
if (DFS(maze, next, end, path, visited)) {
return true;
}
// 如果四个方向都搜索不到终点,则将当前位置从路径中删除
path.pop_back();
return false;
}
// 求解一条迷宫路径函数
void SolveOnePath(Maze& maze, Pos& start, Pos& end) {
vector<Pos> path;
vector<vector<int>> visited(maze.row, vector<int>(maze.col, 0));
// 深度优先搜索求解一条迷宫路径
if (DFS(maze, start, end, path, visited)) {
cout << "迷宫路径如下: " << endl;
for (int i = 0; i < path.size(); i++) {
cout << "(" << path[i].x << "," << path[i].y << ") ";
}
cout << endl;
}
else {
cout << "没有找到迷宫路径!" << endl;
}
}
// 求解所有迷宫路径函数
void SolveAllPaths(Maze& maze, Pos& start, Pos& end) {
stack<Pos> s;
vector<vector<int>> visited(maze.row, vector<int>(maze.col, 0));
s.push(start);
cout << "所有迷宫路径如下: " << endl;
while (!s.empty()) {
Pos cur = s.top();
s.pop();
// 如果当前位置为终点,则输出路径
if (cur.x == end.x && cur.y == end.y) {
cout << "(" << start.x << "," << start.y << ") ";
stack<Pos> temp;
while (!s.empty()) {
temp.push(s.top());
s.pop();
}
while (!temp.empty()) {
cout << "(" << temp.top().x << "," << temp.top().y << ") ";
s.push(temp.top());
temp.pop();
}
cout << "(" << end.x << "," << end.y << ")" << endl;
continue;
}
// 如果当前位置不合法,则跳过
if (!IsValidPos(maze, cur)) {
continue;
}
// 如果当前位置已被访问过,则跳过
if (visited[cur.x][cur.y] == 1) {
continue;
}
// 将当前位置标记为已访问,并加入路径
visited[cur.x][cur.y] = 1;
s.push(cur);
// 分别向上、下、左、右四个方向搜索
Pos next;
next.x = cur.x - 1;
next.y = cur.y;
s.push(next);
next.x = cur.x + 1;
next.y = cur.y;
s.push(next);
next.x = cur.x;
next.y = cur.y - 1;
s.push(next);
next.x = cur.x;
next.y = cur.y + 1;
s.push(next);
}
}
// 求解最短迷宫路径函数
void SolveShortestPath(Maze& maze, Pos& start, Pos& end) {
queue<Pos> q;
vector<vector<int>> visited(maze.row, vector<int>(maze.col, 0));
vector<vector<int>> dist(maze.row, vector<int>(maze.col, 0));
q.push(start);
visited[start.x][start.y] = 1;
while (!q.empty()) {
Pos cur = q.front();
q.pop();
// 如果当前位置为终点,则输出路径长度
if (cur.x == end.x && cur.y == end.y) {
cout << "最短迷宫路径长度为: " << dist[cur.x][cur.y] << endl;
return;
}
// 分别向上、下、左、右四个方向搜索
Pos next;
next.x = cur.x - 1;
next.y = cur.y;
if (IsValidPos(maze, next) && visited[next.x][next.y] == 0) {
visited[next.x][next.y] = 1;
dist[next.x][next.y] = dist[cur.x][cur.y] + 1;
q.push(next);
}
next.x = cur.x + 1;
next.y = cur.y;
if (IsValidPos(maze, next) && visited[next.x][next.y] == 0) {
visited[next.x][next.y] = 1;
dist[next.x][next.y] = dist[cur.x][cur.y] + 1;
q.push(next);
}
next.x = cur.x;
next.y = cur.y - 1;
if (IsValidPos(maze, next) && visited[next.x][next.y] == 0) {
visited[next.x][next.y] = 1;
dist[next.x][next.y] = dist[cur.x][cur.y] + 1;
q.push(next);
}
next.x = cur.x;
next.y = cur.y + 1;
if (IsValidPos(maze, next) && visited[next.x][next.y] == 0) {
visited[next.x][next.y] = 1;
dist[next.x][next.y] = dist[cur.x][cur.y] + 1;
q.push(next);
}
}
cout << "没有找到迷宫路径!" << endl;
}
int main() {
Maze maze;
AddMaze(maze);
ShowMaze(maze);
Pos start, end;
cout << "请输入起点坐标: ";
cin >> start.x >> start.y;
cout << "请输入终点坐标: ";
cin >> end.x >> end.y;
SolveOnePath(maze, start, end);
SolveAllPaths(maze, start, end);
SolveShortestPath(maze, start, end);
return 0;
}
```
注意:以上代码仅供参考,可能存在一些细节问题,具体实现可根据需要进行调整。