简体   繁体   English

C语言中的随机迷宫生成器

[英]Random maze generator in C

I don't know how to make sure the random maze can lead from the entry on the right side to the exit on the left side without any wall block the path. 我不知道如何确保随机迷宫可以从右侧的入口通向左侧的出口,而不会阻塞任何路径。 This is my code I am doing so far. 到目前为止,这是我正在执行的代码。 Can everybody give me a hint or algorithm to achieve the simple maze (entry/exit)? 大家可以给我一个提示或算法来实现简单的迷宫(进入/退出)吗? Thank you! 谢谢! P/S my problem is the maze generator doesn't ensure the path to the exit...(get stuck) 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("");
}

Your problem is that the algorithm you have chosen does not guarantee that there is a path from your entry point to your exit point. 您的问题是您选择的算法不能保证从入口到出口都有一条路径。 Essentially you fill your maze randomly, and that is not going to result in a guaranteed path (indeed it may result in multiple paths). 本质上,您是随机填充迷宫的,这不会导致路径有保证(实际上可能会导致多个路径)。

You want to use a maze generation algorithm. 您要使用迷宫生成算法。 These are a well known class of algorithms that will generate mazes with a solution (in some cases exactly one solution). 这些是众所周知的算法类别,它们会通过一种解决方案(在某些情况下恰好是一种解决方案)生成迷宫。 There is an article and references to a number of such algorithms here: http://en.wikipedia.org/wiki/Maze_generation_algorithm 这里有一篇文章,并引用了许多这样的算法: http : //en.wikipedia.org/wiki/Maze_generation_algorithm

You should use one of the well-established maze generation algorithms. 您应该使用一种公认的迷宫生成算法。 Here is a very short C++ implementation of depth first search (DFS) algorithm: 这是深度优先搜索(DFS)算法的非常简短的C ++实现:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM