写一个用C语言的迷宫自动生成程序,参考prim算法
时间: 2023-10-03 22:10:12 浏览: 92
好的,以下是一个用C语言实现的迷宫自动生成程序,参考prim算法:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 20
#define COL 20
int maze[ROW][COL];
int visited[ROW][COL];
typedef struct
{
int row;
int col;
} Cell;
Cell front;
Cell rear;
Cell queue[ROW*COL];
int qsize;
void init_maze()
{
int i, j;
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
maze[i][j] = 1;
}
}
}
void init_visited()
{
int i, j;
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
visited[i][j] = 0;
}
}
}
void init_queue()
{
qsize = 0;
front.row = -1;
front.col = -1;
rear.row = -1;
rear.col = -1;
}
int is_queue_empty()
{
if (qsize == 0)
{
return 1;
}
else
{
return 0;
}
}
void enqueue(Cell cell)
{
if (is_queue_empty())
{
front = cell;
}
rear = cell;
queue[qsize++] = cell;
}
Cell dequeue()
{
Cell cell = queue[0];
int i;
for (i = 1; i < qsize; i++)
{
queue[i - 1] = queue[i];
}
qsize--;
if (qsize == 0)
{
front.row = -1;
front.col = -1;
rear.row = -1;
rear.col = -1;
}
else
{
front = queue[0];
}
return cell;
}
void print_maze()
{
int i, j;
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
if (maze[i][j] == 1)
{
printf("# ");
}
else
{
printf(" ");
}
}
printf("\n");
}
}
void generate_maze()
{
int i, j;
int row, col;
Cell current;
Cell next;
int dir;
int dirs[4][2] = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };
int count;
srand((unsigned int)time(NULL));
row = rand() % ROW;
col = rand() % COL;
current.row = row;
current.col = col;
visited[row][col] = 1;
while (1)
{
count = 0;
for (i = 0; i < 4; i++)
{
row = current.row + dirs[i][0];
col = current.col + dirs[i][1];
if (row >= 0 && row < ROW && col >= 0 && col < COL &&
visited[row][col] == 0)
{
count++;
}
}
if (count == 0)
{
if (is_queue_empty())
{
break;
}
current = dequeue();
continue;
}
dir = rand() % 4;
for (i = 0; i < 4; i++)
{
row = current.row + dirs[(dir + i) % 4][0];
col = current.col + dirs[(dir + i) % 4][1];
if (row >= 0 && row < ROW && col >= 0 && col < COL &&
visited[row][col] == 0)
{
break;
}
}
maze[current.row][current.col] &= ~(1 << dir);
next.row = row;
next.col = col;
maze[next.row][next.col] &= ~(1 << ((dir + 2) % 4));
visited[next.row][next.col] = 1;
enqueue(current);
current = next;
}
}
int main()
{
init_maze();
init_visited();
init_queue();
generate_maze();
print_maze();
return 0;
}
```
这个程序实现了一个随机迷宫的生成,其中使用了prim算法的思想,通过随机选择一个起始点,然后不断扩展迷宫的边界,最终得到一个完整的迷宫。程序中的maze数组表示迷宫的边界,1表示有墙,0表示可以通过,visited数组表示当前位置是否已经被访问过,queue数组表示需要访问的位置队列。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044736.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)