gpt4 book ai didi

c - C中的随机迷宫生成器

转载 作者:太空狗 更新时间:2023-10-29 15:04:05 29 4
gpt4 key购买 nike

我不知道如何确保随机迷宫可以从右侧的入口通向左侧的导出,而没有任何墙壁阻挡路径。这是我目前正在做的代码。每个人都可以给我一个提示或算法来实现简单的迷宫(进入/退出)吗?谢谢!P/S 我的问题是迷宫生成器无法确保通往导出的路径...(卡住)

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define SIZE 12
void mazeGenerator(char [][SIZE]);

int main(void)
{
char maze[SIZE][SIZE];
srand((unsigned int)time(NULL));
mazeGenerator(maze);
return 0;
}
void mazeGenerator(char a[SIZE][SIZE])
{
size_t row,column = 0, r;

// initialize '#' to all positions of left-hand wall
for ( row = 0; row < SIZE; ++row )
{
a[row][column] = '#';
}
// initialize '#' to all positions of left-hand wall
for ( row = 0; row < SIZE; ++row )
{
a[row][SIZE - 1] = '#';
}

// initialize '.' to left-hand wall random positions from 1 -> 10
row = rand() % 11 + 1;
a[row][0] = '.';

// initialize '.' to right-hand wall random positions from 1 -> 10
row = rand() % 11 + 1;
a[row][SIZE - 1] = '.';

// intialize '#' to all positions of top maze
for (column = 1; column < SIZE - 1; ++column)
{
a[0][column] = '#';
}

// intialize '#' to all positions of bottom maze
for (column = 1; column < SIZE - 1; ++column)
{
a[SIZE - 1][column] = '#';
}

// print maze
puts("");
puts("** Maze Generator by Huy Le **\n");
for (row = 0; row < SIZE; ++row)
{
for (column = 0; column < SIZE; ++column)
{
printf_s("%2c",a[row][column]);
}
puts("");
}
puts("");
}

最佳答案

您应该使用一种行之有效的迷宫生成算法。这是深度优先搜索 (DFS) 算法的一个非常简短的 C++ 实现:

http://github.com/corporateshark/random-maze-generator

关于c - C中的随机迷宫生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22747974/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com