用c语言随机生成一个迷宫的代码
时间: 2024-05-04 19:21:56 浏览: 15
这是一个简单的用 C 语言随机生成迷宫的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 10
#define COL 10
// 定义迷宫结构体
typedef struct {
int row;
int col;
int visited;
int top_wall;
int bottom_wall;
int left_wall;
int right_wall;
} Cell;
// 初始化迷宫
void initMaze(Cell maze[][COL]) {
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
maze[i][j].row = i;
maze[i][j].col = j;
maze[i][j].visited = 0;
maze[i][j].top_wall = 1;
maze[i][j].bottom_wall = 1;
maze[i][j].left_wall = 1;
maze[i][j].right_wall = 1;
}
}
}
// 随机选择一个未访问的相邻单元格
Cell* chooseNeighbor(Cell maze[][COL], Cell* cell) {
Cell* neighbors[4] = {NULL};
int count = 0;
if (cell->row > 0 && maze[cell->row - 1][cell->col].visited == 0) {
neighbors[count++] = &maze[cell->row - 1][cell->col];
}
if (cell->row < ROW - 1 && maze[cell->row + 1][cell->col].visited == 0) {
neighbors[count++] = &maze[cell->row + 1][cell->col];
}
if (cell->col > 0 && maze[cell->row][cell->col - 1].visited == 0) {
neighbors[count++] = &maze[cell->row][cell->col - 1];
}
if (cell->col < COL - 1 && maze[cell->row][cell->col + 1].visited == 0) {
neighbors[count++] = &maze[cell->row][cell->col + 1];
}
if (count == 0) {
return NULL;
}
int randomIndex = rand() % count;
return neighbors[randomIndex];
}
// 移除墙壁
void removeWall(Cell* curr, Cell* neighbor) {
int rowDiff = curr->row - neighbor->row;
int colDiff = curr->col - neighbor->col;
if (rowDiff == 1) {
curr->top_wall = 0;
neighbor->bottom_wall = 0;
} else if (rowDiff == -1) {
curr->bottom_wall = 0;
neighbor->top_wall = 0;
} else if (colDiff == 1) {
curr->left_wall = 0;
neighbor->right_wall = 0;
} else if (colDiff == -1) {
curr->right_wall = 0;
neighbor->left_wall = 0;
}
}
// 生成迷宫
void generateMaze(Cell maze[][COL], Cell* start) {
start->visited = 1;
Cell* curr = start;
while (1) {
Cell* neighbor = chooseNeighbor(maze, curr);
if (neighbor != NULL) {
neighbor->visited = 1;
removeWall(curr, neighbor);
curr = neighbor;
} else {
int found = 0;
for (int i = 0; i < ROW && !found; i++) {
for (int j = 0; j < COL && !found; j++) {
if (maze[i][j].visited == 0) {
curr = &maze[i][j];
curr->visited = 1;
removeWall(curr, chooseNeighbor(maze, curr));
found = 1;
}
}
}
if (!found) {
break;
}
}
}
}
// 打印迷宫
void printMaze(Cell maze[][COL]) {
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
printf("+");
if (maze[i][j].top_wall == 1) {
printf("---");
} else {
printf(" ");
}
printf("+");
}
printf("\n");
for (int j = 0; j < COL; j++) {
if (maze[i][j].left_wall == 1) {
printf("|");
} else {
printf(" ");
}
printf(" ");
if (maze[i][j].right_wall == 1) {
printf("|");
} else {
printf(" ");
}
}
printf("\n");
}
for (int j = 0; j < COL; j++) {
printf("+---");
}
printf("+\n");
}
int main() {
srand(time(NULL));
Cell maze[ROW][COL];
initMaze(maze);
generateMaze(maze, &maze[0][0]);
printMaze(maze);
return 0;
}
```
这个代码使用了深度优先搜索算法来生成迷宫。在初始化迷宫时,每个单元格都被标记为未访问,并且四面都有墙。然后从起点开始,随机选择一个相邻的未访问单元格,并移除两个单元格之间的墙壁,然后继续访问这个单元格。如果没有未访问的相邻单元格,就回到之前的单元格,直到所有的单元格都被访问过为止。最后,打印出生成的迷宫。