用C++实现一个随机迷宫生成器的代码
时间: 2024-03-25 16:40:24 浏览: 27
以下是一个使用深度优先搜索(DFS)算法来生成随机迷宫的 C++ 代码:
```c++
#include <iostream>
#include <stack>
#include <cstdlib>
#include <ctime>
using namespace std;
const int MAZE_SIZE = 10;
enum Direction { NORTH, EAST, SOUTH, WEST };
struct Cell {
bool visited;
bool walls[4];
};
void init_maze(Cell maze[][MAZE_SIZE]) {
for (int i = 0; i < MAZE_SIZE; i++) {
for (int j = 0; j < MAZE_SIZE; j++) {
maze[i][j].visited = false;
maze[i][j].walls[NORTH] = true;
maze[i][j].walls[EAST] = true;
maze[i][j].walls[SOUTH] = true;
maze[i][j].walls[WEST] = true;
}
}
}
bool is_valid(int row, int col) {
return row >= 0 && row < MAZE_SIZE && col >= 0 && col < MAZE_SIZE;
}
void remove_wall(Cell& current, Cell& next) {
if (current.walls[NORTH] && current.walls[SOUTH] &&
current.walls[EAST] && !current.walls[WEST] &&
next.walls[NORTH] && next.walls[SOUTH] &&
next.walls[WEST] && !next.walls[EAST]) {
current.walls[WEST] = false;
next.walls[EAST] = false;
} else if (current.walls[NORTH] && current.walls[SOUTH] &&
!current.walls[EAST] && current.walls[WEST] &&
next.walls[NORTH] && next.walls[SOUTH] &&
!next.walls[WEST] && next.walls[EAST]) {
current.walls[NORTH] = false;
next.walls[SOUTH] = false;
} else if (current.walls[NORTH] && !current.walls[SOUTH] &&
current.walls[EAST] && current.walls[WEST] &&
next.walls[SOUTH] && !next.walls[NORTH] &&
next.walls[WEST] && next.walls[EAST]) {
current.walls[SOUTH] = false;
next.walls[NORTH] = false;
} else if (!current.walls[NORTH] && current.walls[SOUTH] &&
current.walls[EAST] && current.walls[WEST] &&
!next.walls[SOUTH] && next.walls[NORTH] &&
next.walls[WEST] && next.walls[EAST]) {
current.walls[NORTH] = false;
next.walls[SOUTH] = false;
}
}
void generate_maze(Cell maze[][MAZE_SIZE], int start_row, int start_col) {
stack<pair<int, int>> stack;
stack.push(make_pair(start_row, start_col));
maze[start_row][start_col].visited = true;
srand(time(NULL));
while (!stack.empty()) {
int row = stack.top().first;
int col = stack.top().second;
stack.pop();
int directions[4] = { NORTH, EAST, SOUTH, WEST };
for (int i = 0; i < 4; i++) {
int index = rand() % 4;
int direction = directions[index];
directions[index] = directions[i];
int next_row = row, next_col = col;
switch (direction) {
case NORTH:
next_row--;
break;
case EAST:
next_col++;
break;
case SOUTH:
next_row++;
break;
case WEST:
next_col--;
break;
}
if (is_valid(next_row, next_col) && !maze[next_row][next_col].visited) {
maze[next_row][next_col].visited = true;
remove_wall(maze[row][col], maze[next_row][next_col]);
stack.push(make_pair(next_row, next_col));
}
}
}
}
void print_maze(Cell maze[][MAZE_SIZE]) {
for (int i = 0; i < MAZE_SIZE; i++) {
for (int j = 0; j < MAZE_SIZE; j++) {
if (maze[i][j].walls[NORTH]) {
cout << "+---";
} else {
cout << "+ ";
}
}
cout << "+" << endl;
for (int j = 0; j < MAZE_SIZE; j++) {
if (maze[i][j].walls[WEST]) {
cout << "| ";
} else {
cout << " ";
}
}
cout << "|" << endl;
}
for (int j = 0; j < MAZE_SIZE; j++) {
cout << "+---";
}
cout << "+" << endl;
}
int main() {
Cell maze[MAZE_SIZE][MAZE_SIZE];
init_maze(maze);
generate_maze(maze, 0, 0);
print_maze(maze);
return 0;
}
```
这段代码中,我们使用了一个栈来存储当前正在访问的单元格,并随机选择一个未访问的相邻单元格进行访问。在访问过程中,我们使用 `remove_wall()` 函数来移除当前单元格和相邻单元格之间的墙壁。最后,我们使用 `print_maze()` 函数来打印生成的随机迷宫。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)